# 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 #include #include 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"; }; }