aboutsummaryrefslogtreecommitdiffstats
path: root/layers/nix.nix
blob: 67639bea4c5feffcb1b85b3ba47cc9e2eb911924 (plain)
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
# 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/disk/by-label/fc-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.
  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" ];
  };
}