git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #170098 from helsinki-systems/feat/systemd-stage-1-secrets

nixos/systemd-stage-1: Add initrd secrets support

commitc3932802025fa431e347c0f64cf22f1ac4735c87 authorLinus Heckemann <git@sphalerite.org> dateSat 30 Apr 2022, 14:56 (5 years ago) parent153690dfa2c9b4
3 files changed+39−2
nixos/modules/module-list.nix+2−1 nixos/modules/system/boot/systemd/initrd-secrets.nix+36 nixos/tests/systemd-initrd-luks-keyfile.nix+1−1
nixos/modules/module-list.nix+2−1
@@ -1183,13 +1183,14 @@
11831183 ./system/boot/stage-2.nix
11841184 ./system/boot/systemd.nix
11851185 ./system/boot/systemd/coredump.nix
1186+ ./system/boot/systemd/initrd-secrets.nix
1187+ ./system/boot/systemd/initrd.nix
11861188 ./system/boot/systemd/journald.nix
11871189 ./system/boot/systemd/logind.nix
11881190 ./system/boot/systemd/nspawn.nix
11891191 ./system/boot/systemd/shutdown.nix
11901192 ./system/boot/systemd/tmpfiles.nix
11911193 ./system/boot/systemd/user.nix
1192− ./system/boot/systemd/initrd.nix
11931194 ./system/boot/timesyncd.nix
11941195 ./system/boot/tmp.nix
11951196 ./system/etc/etc-activation.nix
nixos/modules/system/boot/systemd/initrd-secrets.nixnew file+36
@@ -0,0 +1,36 @@
1+{ config, pkgs, lib, ... }:
2+
3+{
4+ config = lib.mkIf (config.boot.initrd.enable && config.boot.initrd.systemd.enable) {
5+ # Copy secrets into the initrd if they cannot be appended
6+ boot.initrd.systemd.contents = lib.mkIf (!config.boot.loader.supportsInitrdSecrets)
7+ (lib.mapAttrs' (dest: source: lib.nameValuePair "/.initrd-secrets/${dest}" { source = if source == null then dest else source; }) config.boot.initrd.secrets);
8+
9+ # Copy secrets to their respective locations
10+ boot.initrd.systemd.services.initrd-nixos-copy-secrets = lib.mkIf (config.boot.initrd.secrets != {}) {
11+ description = "Copy secrets into place";
12+ # Run as early as possible
13+ wantedBy = [ "sysinit.target" ];
14+ before = [ "cryptsetup-pre.target" ];
15+ unitConfig.DefaultDependencies = false;
16+
17+ # We write the secrets to /.initrd-secrets and move them because this allows
18+ # secrets to be written to /run. If we put the secret directly to /run and
19+ # drop this service, we'd mount the /run tmpfs over the secret, making it
20+ # invisible in stage 2.
21+ script = ''
22+ for secret in $(cd /.initrd-secrets; find . -type f); do
23+ mkdir -p "$(dirname "/$secret")"
24+ cp "/.initrd-secrets/$secret" "/$secret"
25+ done
26+ '';
27+
28+ unitConfig = {
29+ Type = "oneshot";
30+ RemainAfterExit = true;
31+ };
32+ };
33+ # The script needs this
34+ boot.initrd.systemd.extraBin.find = "${pkgs.findutils}/bin/find";
35+ };
36+}
nixos/tests/systemd-initrd-luks-keyfile.nix+1−1
@@ -32,7 +32,7 @@ in {
3232 };
3333 };
3434 virtualisation.bootDevice = "/dev/mapper/cryptroot";
35− boot.initrd.systemd.contents."/etc/cryptroot.key".source = keyfile;
35+ boot.initrd.secrets."/etc/cryptroot.key" = keyfile;
3636 };
3737 };
3838