1 # opinionated module that can be used to build nixos images with
2 # a dm-verity protected nix store
10 cfg = config.image.repart.verityStore;
12 verityMatchKey = "store";
14 # TODO: make these and other arch mappings available from systemd-lib for example
18 "x86_64" = "usr-x86-64";
19 "arm64" = "usr-arm64";
21 ."${pkgs.stdenv.hostPlatform.linuxArch}";
25 "x86_64" = "usr-x86-64-verity";
26 "arm64" = "usr-arm64-verity";
28 ."${pkgs.stdenv.hostPlatform.linuxArch}";
32 pkgs.buildPackages.writers.writePython3Bin "assert_uki_repart_match.py"
34 flakeIgnore = [ "E501" ]; # ignores PEP8's line length limit of 79 (black defaults to 88 characters)
37 builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [
38 partitionTypes.usr-verity
39 ] (builtins.readFile ./assert_uki_repart_match.py)
43 options.image.repart.verityStore = {
44 enable = lib.mkEnableOption "building images with a dm-verity protected nix store";
46 ukiPath = lib.mkOption {
48 default = "/EFI/Linux/${config.system.boot.loader.ukiFile}";
49 defaultText = "/EFI/Linux/\${config.system.boot.loader.ukiFile}";
51 Specify the location on the ESP where the UKI is placed.
60 Specify the attribute name of the ESP.
63 store-verity = lib.mkOption {
65 default = "10-store-verity";
67 Specify the attribute name of the store's dm-verity hash partition.
70 store = lib.mkOption {
74 Specify the attribute name of the store partition.
80 config = lib.mkIf cfg.enable {
81 boot.initrd.systemd.dmVerity.enable = true;
83 image.repart.partitions = {
84 # dm-verity hash partition
85 ${cfg.partitionIds.store-verity}.repartConfig = {
86 Type = partitionTypes.usr-verity;
88 VerityMatchKey = lib.mkDefault verityMatchKey;
89 Label = lib.mkDefault "store-verity";
91 # dm-verity data partition that contains the nix store
92 ${cfg.partitionIds.store} = {
93 storePaths = [ config.system.build.toplevel ];
95 Type = partitionTypes.usr;
97 Format = lib.mkDefault "erofs";
98 VerityMatchKey = lib.mkDefault verityMatchKey;
99 Label = lib.mkDefault "store";
107 # intermediate system image without ESP
109 (config.system.build.image.override {
110 # always disable compression for the intermediate image
111 compression.enable = false;
115 # make it easier to identify the intermediate image in build logs
116 pname = "${previousAttrs.pname}-intermediate";
118 # do not prepare the ESP, this is done in the final image
119 systemdRepartFlags = previousAttrs.systemdRepartFlags ++ [ "--defer-partitions=esp" ];
123 # UKI with embedded usrhash from intermediateImage
126 inherit (config.system.boot.loader) ukiFile;
127 cmdline = "init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}";
129 # override the default UKI
131 pkgs.runCommand ukiFile
133 nativeBuildInputs = [
141 # Extract the usrhash from the output of the systemd-repart invocation for the intermediate image.
143 '.[] | select(.type=="${partitionTypes.usr-verity}") | .roothash' \
144 ${config.system.build.intermediateImage}/repart-output.json
147 # Build UKI with the embedded usrhash.
149 --config=${config.boot.uki.configFile} \
150 --cmdline="${cmdline} usrhash=$usrhash" \
151 --output="$out/${ukiFile}"
155 # final system image that is created from the intermediate image by injecting the UKI from above
157 (config.system.build.image.override {
158 # continue building with existing intermediate image
162 finalAttrs: previousAttrs: {
163 # add entry to inject UKI into ESP
164 finalPartitions = lib.recursiveUpdate previousAttrs.finalPartitions {
165 ${cfg.partitionIds.esp}.contents = {
166 "${cfg.ukiPath}".source = "${config.system.build.uki}/${config.system.boot.loader.ukiFile}";
170 nativeBuildInputs = previousAttrs.nativeBuildInputs ++ [
177 # check that we build the final image with the same intermediate image for
178 # which the injected UKI was built by comparing the UKI cmdline with the repart output
179 # of the intermediate image
181 # This is necessary to notice incompatible substitutions of
182 # non-reproducible store paths, for example when working with distributed
183 # builds, or when offline-signing the UKI.
184 ukify --json=short inspect ${config.system.build.uki}/${config.system.boot.loader.ukiFile} \
185 | assert_uki_repart_match.py "${config.system.build.intermediateImage}/repart-output.json"
187 # copy the uncompressed intermediate image, so that systemd-repart picks it up
188 cp -v ${config.system.build.intermediateImage}/${config.image.repart.imageFileBasename}.raw .
189 chmod +w ${config.image.repart.imageFileBasename}.raw
192 # replace "TBD" with the original roothash values
194 mv -v repart-output{.json,_orig.json}
196 jq --slurp --indent -1 \
197 '.[0] as $intermediate | .[1] as $final
198 | $intermediate | map(select(.roothash != null) | { "uuid":.uuid,"roothash":.roothash }) as $uuids
202 | sort_by(.offset)' \
203 ${config.system.build.intermediateImage}/repart-output.json \
204 repart-output_orig.json \
207 rm -v repart-output_orig.json
214 meta.maintainers = with lib.maintainers; [