10 imports = [ ./etc.nix ];
12 config = lib.mkMerge [
15 system.activationScripts.etc = lib.stringAfter [
19 ] config.system.build.etcActivationCommands;
22 (lib.mkIf config.system.etc.overlay.enable {
26 assertion = config.boot.initrd.systemd.enable;
27 message = "`system.etc.overlay.enable` requires `boot.initrd.systemd.enable`";
31 (!config.system.etc.overlay.mutable)
32 -> (config.systemd.sysusers.enable || config.services.userborn.enable);
33 message = "`!system.etc.overlay.mutable` requires `systemd.sysusers.enable` or `services.userborn.enable`";
37 (config.system.switch.enable)
38 -> (lib.versionAtLeast config.boot.kernelPackages.kernel.version "6.6");
39 message = "switchable systems with `system.etc.overlay.enable` require a newer kernel, at least version 6.6";
43 boot.initrd.availableKernelModules = [
49 boot.initrd.systemd = {
52 where = "/run/nixos-etc-metadata";
53 what = "/etc-metadata-image";
57 # Since this unit depends on the nix store being mounted, it cannot
58 # be a dependency of local-fs.target, because if it did, we'd have
59 # local-fs.target ordered after the nix store mount which would cause
60 # things like network.target to only become active after the nix store
62 # This breaks for instance setups where sshd needs to be up before
63 # any encrypted disks can be mounted.
64 DefaultDependencies = false;
70 config.boot.initrd.systemd.services.initrd-find-etc.name
73 config.boot.initrd.systemd.services.initrd-find-etc.name
75 requiredBy = [ "initrd-fs.target" ];
76 before = [ "initrd-fs.target" ];
79 where = "/sysroot/etc";
82 options = lib.concatStringsSep "," (
87 "lowerdir=/run/nixos-etc-metadata::/etc-basedir"
89 ++ lib.optionals config.system.etc.overlay.mutable [
91 "upperdir=/sysroot/.rw-etc/upper"
92 "workdir=/sysroot/.rw-etc/work"
94 ++ lib.optionals (!config.system.etc.overlay.mutable) [
98 requiredBy = [ "initrd-fs.target" ];
99 before = [ "initrd-fs.target" ];
102 config.boot.initrd.systemd.services.initrd-find-etc.name
104 ++ lib.optionals config.system.etc.overlay.mutable [
105 config.boot.initrd.systemd.services."rw-etc".name
109 config.boot.initrd.systemd.services.initrd-find-etc.name
111 ++ lib.optionals config.system.etc.overlay.mutable [
112 config.boot.initrd.systemd.services."rw-etc".name
115 RequiresMountsFor = [
117 "/run/nixos-etc-metadata"
119 DefaultDependencies = false;
123 services = lib.mkMerge [
124 (lib.mkIf config.system.etc.overlay.mutable {
126 requiredBy = [ "initrd-fs.target" ];
127 before = [ "initrd-fs.target" ];
129 DefaultDependencies = false;
130 RequiresMountsFor = "/sysroot";
135 /bin/mkdir -p -m 0755 /sysroot/.rw-etc/upper /sysroot/.rw-etc/work
142 description = "Find the path to the etc metadata image and based dir";
144 config.boot.initrd.systemd.services.initrd-find-nixos-closure.name
147 config.boot.initrd.systemd.services.initrd-find-nixos-closure.name
149 before = [ "shutdown.target" ];
150 conflicts = [ "shutdown.target" ];
151 requiredBy = [ "initrd.target" ];
153 DefaultDependencies = false;
154 RequiresMountsFor = "/sysroot/nix/store";
158 RemainAfterExit = true;
165 closure="$(realpath /nixos-closure)"
167 metadata_image="$(${pkgs.chroot-realpath}/bin/chroot-realpath /sysroot "$closure/etc-metadata-image")"
168 ln -s "/sysroot$metadata_image" /etc-metadata-image
170 basedir="$(${pkgs.chroot-realpath}/bin/chroot-realpath /sysroot "$closure/etc-basedir")"
171 ln -s "/sysroot$basedir" /etc-basedir