aboutsummaryrefslogtreecommitdiffstats
path: root/layers/nix.nix
diff options
context:
space:
mode:
Diffstat (limited to 'layers/nix.nix')
-rw-r--r--layers/nix.nix120
1 files changed, 120 insertions, 0 deletions
diff --git a/layers/nix.nix b/layers/nix.nix
new file mode 100644
index 0000000..8e00d13
--- /dev/null
+++ b/layers/nix.nix
@@ -0,0 +1,120 @@
+# 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
+ inherit (config.fileSystems."/nix/store".overlay) lowerdir;
+in
+{
+ nix.enable = true;
+ fc.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/vda"; # the first drive, 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.
+ fileSystems."/nix/store".overlay = {
+ lowerdir = [ "/nix/.host-store" ];
+ upperdir = "/nix/.rw-store/store";
+ workdir = "/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 a lower layer 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.
+ boot.initrd.systemd.services.fc-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 = ''
+ cd /sysroot/nix/.rw-store/store 2>/dev/null || exit 0
+ for p in *; do
+ for d in ${toString (map (d: "/sysroot" + d) lowerdir)}; do
+ if test -e "$d/$p"; then
+ rm -rf "$p"
+ break
+ fi
+ done
+ done
+ '';
+ };
+ # For the host's store, in the initrd.
+ boot.initrd.systemd.extraBin."mount.vsock9p" = config.fc.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 (fc.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.
+ script = ''
+ for arg in $(cat /proc/cmdline); do
+ case $arg in fc.registration=*)
+ ${config.nix.package.out}/bin/nix-store --load-db < "''${arg#*=}"
+ esac
+ done
+ ${config.nix.package.out}/bin/nix-store --verify
+ '';
+ };
+ # 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" ];
+ };
+}