1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# nix, as a layer: put it into the layers, before the ones that need it,
# for an image of its own, the same for all of them:
# --arg layers '[ layers/nix.nix ./my-layer.nix ]'
# Without it, the guest has neither nix nor nix-daemon, and its store is
# its images, read-only. With it, the run script requires --store.
{ config, lib, pkgs, ... }:
let
lowerdir = config.rig.storeLowerdirs;
in
{
nix.enable = true;
rig.nix = true;
# The store, on top of the guest's own closure from images (which reading
# is much faster than reading the host's store): the host's store,
# read-only via 9P over vsock (see mount.vsock9p and the run script),
# for anything else, like nixpkgs; then the first drive on top, so nix
# can add to the store (one that persists, see the run script). The
# host's store doesn't change under a path, so 9P may cache freely.
# Store paths don't collide, so the order of the lower layers doesn't
# matter.
fileSystems."/nix/.host-store" = {
device = "565";
fsType = "vsock9p";
options = [ "ro" "cache=loose" ];
noCheck = true;
neededForBoot = true;
};
fileSystems."/nix/.rw-store" = {
device = "/dev/disk/by-label/rig-store"; # see the run script
fsType = "ext4";
options = [ "noatime" ];
noCheck = true;
neededForBoot = true;
};
# nix's database goes there, too, so it matches what's in the store.
fileSystems."/nix/var" = {
device = "/nix/.rw-store/var";
fsType = "none";
options = [ "bind" ];
depends = [ "/nix/.rw-store" ];
};
systemd.services.nix-var-dir = {
requiredBy = [ "nix-var.mount" ];
before = [ "nix-var.mount" ];
unitConfig.DefaultDependencies = false;
unitConfig.RequiresMountsFor = "/nix/.rw-store";
serviceConfig.Type = "oneshot";
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/mkdir -p -m 0755 /nix/.rw-store/var";
};
# The images are an overlay already: this is one image more than the
# base system, so there are at least two.
rig.storeLowerdirs = [ "/nix/.host-store" ];
# The upper layer, which rig-rw-store-unshadow (below) creates.
fileSystems."/nix/store" = {
depends = [ "/nix/.rw-store" ];
options = [ "upperdir=/sysroot/nix/.rw-store/store" "workdir=/sysroot/nix/.rw-store/work" ];
};
# nix in the guest deletes a path in the store that isn't in its
# database before it adds it, like the host's store's paths. On the
# overlay, deleting a lower layer's path leaves a whiteout in the rw-store,
# or an opaque directory, which is what nix adds again, if it gets that
# far. With a persistent rw-store, that hides the path from any
# later image, too. Any path that an image has is complete there, and
# the same as anything of that name in the rw-store, so drop such paths
# from the rw-store before mounting the overlay. Not so for the host's
# store, which can lose a path any time (its garbage collection), while
# the guest's nix still has it registered (then nix-store --verify
# fails): drop only whiteouts, which would hide the host's path, and
# keep real copies.
boot.initrd.systemd.services.rig-rw-store-unshadow = {
requiredBy = [ "sysroot-nix-store.mount" ];
before = [ "sysroot-nix-store.mount" ];
unitConfig.DefaultDependencies = false;
unitConfig.RequiresMountsFor = map (d: "/sysroot" + d) ([ "/nix/.rw-store" ] ++ lowerdir);
serviceConfig.Type = "oneshot";
script = ''
mkdir -p -m 0755 /sysroot/nix/.rw-store/store /sysroot/nix/.rw-store/work
cd /sysroot/nix/.rw-store/store
for p in *; do
for d in ${toString (map (d: "/sysroot" + d) (lib.remove "/nix/.host-store" lowerdir))}; do
if test -e "$d/$p"; then
rm -rf "$p"
continue 2
fi
done
# Whiteouts (character devices, which no store path is) hide the
# host's path, or nothing.
if test -c "$p"; then
rm -f "$p"
fi
done
'';
};
# For the host's store, in the initrd.
boot.initrd.systemd.extraBin."mount.vsock9p" = config.rig.mountVsock9p;
boot.initrd.systemd.storePaths = [ pkgs.socat pkgs.util-linux ];
# nix, with the host's nixpkgs for nix-shell -p and the like, at
# /nixpkgs (see below). Root goes through the daemon too, so the
# paths of the guest's closure get registered (rig.registration, see
# the run script) before anything uses the store, and only once
# something does, not at boot. The rest of the host's store is
# there, too, but not registered.
nix.nixPath = [ "nixpkgs=/nixpkgs" ];
environment.variables.NIX_REMOTE = "daemon";
systemd.services.nix-load-db = {
requiredBy = [ "nix-daemon.service" ];
before = [ "nix-daemon.service" ];
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
# With a persistent store, paths from an earlier image or host
# store may be gone; --verify removes them from the database. Not
# those that other valid paths refer to, which it reports, and then
# fails, but that's no reason to leave the guest without nix: a build
# needing one of them fails, and says why.
script = ''
for arg in $(cat /proc/cmdline); do
case $arg in rig.registration=*)
${config.nix.package.out}/bin/nix-store --load-db < "''${arg#*=}"
esac
done
${config.nix.package.out}/bin/nix-store --verify || echo "nix-load-db: nix-store --verify left errors (above)" >&2
'';
};
# The host's nixpkgs, wherever it is (see the run script): no copy
# to the store needed, which is what made evaluating this take ~20s.
fileSystems."/nixpkgs" = {
device = "566";
fsType = "vsock9p";
noCheck = true;
options = [ "ro" "x-systemd.automount" "nofail" ];
};
# 9P with cache=loose (the host's store, and /nixpkgs with
# --nixpkgs-cache=loose) never asks the host again: a file changed on
# the host keeps its old size and contents in the guest, and a name
# looked up before the host had it stays missing, until the guest drops
# its cached dentries and inodes, which rig-drop-caches lets anyone do
# (setuid; no input, it only ever writes 2 to drop_caches).
security.wrappers.rig-drop-caches = {
setuid = true;
owner = "root";
group = "root";
source = lib.getExe (pkgs.writeCBin "rig-drop-caches" ''
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
sync();
int fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
if (fd < 0 || write(fd, "2\n", 2) != 2) {
perror("rig-drop-caches: /proc/sys/vm/drop_caches");
return 1;
}
return 0;
}
'');
};
# And the run script has it done via vsock port 1235 when the host's
# nixpkgs checkout changes its commit (with --nixpkgs-cache).
systemd.sockets.rig-drop-caches = {
wantedBy = [ "sockets.target" ];
socketConfig = {
ListenStream = "vsock::1235";
Accept = true;
MaxConnections = 1;
};
};
systemd.services."rig-drop-caches@" = {
serviceConfig.StandardInput = "socket";
script = "/run/wrappers/bin/rig-drop-caches";
};
}
|