git.iko.soy/typeable/nixpkgs
on master help sign in
Add facility to append secrets to the initrd
commit59c097730030f12398fb9d8c70f93571f589e694 authorShea Levy <shea@shealevy.com> dateSun 2 Apr 2017, 14:45 (10 years ago) parentff4fa63
3 files changed+100−3
nixos/modules/system/activation/top-level.nix+2 nixos/modules/system/boot/stage-1-init.sh+8 nixos/modules/system/boot/stage-1.nix+90−3
nixos/modules/system/activation/top-level.nix+2
@@ -53,6 +53,8 @@ let
5353
5454 ln -s ${config.system.build.initialRamdisk}/initrd $out/initrd
5555
56+ ln -s ${config.system.build.initialRamdiskSecretAppender}/bin/append-initrd-secrets $out
57+
5658 ln -s ${config.hardware.firmware}/lib/firmware $out/firmware
5759 ''}
5860
nixos/modules/system/boot/stage-1-init.sh+8
@@ -8,6 +8,14 @@ export LD_LIBRARY_PATH=@extraUtils@/lib
88export PATH=@extraUtils@/bin
99ln -s @extraUtils@/bin /bin
1010
11+# Copy the secrets to their needed location
12+if [ -d "@extraUtils@/secrets" ]; then
13+ for secret in $(cd "@extraUtils@/secrets"; find . -type f); do
14+ mkdir -p $(dirname "/$secret")
15+ ln -s "@extraUtils@/secrets/$secret" "$secret"
16+ done
17+fi
18+
1119# Stop LVM complaining about fd3
1220export LVM_SUPPRESS_FD_WARNINGS=true
1321
nixos/modules/system/boot/stage-1.nix+90−3
@@ -82,6 +82,17 @@ let
8282 copy_bin_and_libs ${pkgs.e2fsprogs}/sbin/resize2fs
8383 ''}
8484
85+ # Copy secrets if needed.
86+ ${optionalString (!config.boot.loader.supportsInitrdSecrets)
87+ (concatStringsSep "\n" (mapAttrsToList (dest: source:
88+ let source' = if source == null then dest else source; in
89+ ''
90+ mkdir -p $(dirname "$out/secrets/${dest}")
91+ cp -a ${source'} "$out/secrets/${dest}"
92+ ''
93+ ) config.boot.initrd.secrets))
94+ }
95+
8596 ${config.boot.initrd.extraUtilsCommands}
8697
8798 # Copy ld manually since it isn't detected correctly
@@ -242,6 +253,52 @@ let
242253 ];
243254 };
244255
256+ # Script to add secret files to the initrd at bootloader update time
257+ initialRamdiskSecretAppender =
258+ pkgs.writeScriptBin "append-initrd-secrets"
259+ ''
260+ #!${pkgs.bash}/bin/bash -e
261+ function usage {
262+ echo "USAGE: $0 INITRD_FILE" >&2
263+ echo "Appends this configuration's secrets to INITRD_FILE" >&2
264+ }
265+
266+ if [ $# -ne 1 ]; then
267+ usage
268+ exit 1
269+ fi
270+
271+ if [ "$1"x = "--helpx" ]; then
272+ usage
273+ exit 0
274+ fi
275+
276+ ${lib.optionalString (config.boot.initrd.secrets == {})
277+ "exit 0"}
278+
279+ export PATH=${pkgs.coreutils}/bin:${pkgs.cpio}/bin:${pkgs.gzip}/bin:${pkgs.findutils}/bin
280+
281+ function cleanup {
282+ if [ -n "$tmp" -a -d "$tmp" ]; then
283+ rm -fR "$tmp"
284+ fi
285+ }
286+ trap cleanup EXIT
287+
288+ tmp=$(mktemp -d initrd-secrets.XXXXXXXXXX)
289+
290+ ${lib.concatStringsSep "\n" (mapAttrsToList (dest: source:
291+ let source' = if source == null then dest else toString source; in
292+ ''
293+ mkdir -p $(dirname "$tmp/${dest}")
294+ cp -a ${source'} "$tmp/${dest}"
295+ ''
296+ ) config.boot.initrd.secrets)
297+ }
298+
299+ (cd "$tmp" && find . | cpio -H newc -o) | gzip >>"$1"
300+ '';
301+
245302in
246303
247304{
@@ -370,6 +427,25 @@ in
370427 example = "xz";
371428 };
372429
430+ boot.initrd.secrets = mkOption
431+ { internal = true;
432+ default = {};
433+ type = types.attrsOf (types.nullOr types.path);
434+ description =
435+ ''
436+ Secrets to append to the initrd. The attribute name is the
437+ path the secret should have inside the initrd, the value
438+ is the path it should be copied from (or null for the same
439+ path inside and out).
440+ '';
441+ example = literalExample
442+ ''
443+ { "/etc/dropbear/dropbear_rsa_host_key" =
444+ ./secret-dropbear-key;
445+ }
446+ '';
447+ };
448+
373449 boot.initrd.supportedFilesystems = mkOption {
374450 default = [ ];
375451 example = [ "btrfs" ];
@@ -377,6 +453,18 @@ in
377453 description = "Names of supported filesystem types in the initial ramdisk.";
378454 };
379455
456+ boot.loader.supportsInitrdSecrets = mkOption
457+ { internal = true;
458+ default = false;
459+ type = types.bool;
460+ description =
461+ ''
462+ Whether the bootloader setup runs append-initrd-secrets.
463+ If not, any needed secrets must be copied into the initrd
464+ and thus added to the store.
465+ '';
466+ };
467+
380468 fileSystems = mkOption {
381469 options.neededForBoot = mkOption {
382470 default = false;
@@ -404,9 +492,8 @@ in
404492 }
405493 ];
406494
407− system.build.bootStage1 = bootStage1;
408− system.build.initialRamdisk = initialRamdisk;
409− system.build.extraUtils = extraUtils;
495+ system.build =
496+ { inherit bootStage1 initialRamdisk initialRamdiskSecretAppender extraUtils; };
410497
411498 system.requiredKernelConfig = with config.lib.kernelConfig; [
412499 (isYes "TMPFS")