diff options
author | nikstur <nikstur@outlook.com> | 2024-03-08 15:23:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-08 15:23:45 +0100 |
commit | 4fc409b9776be490c8823f616fe0ef10b2b49161 (patch) | |
tree | e908dc8ad1f0f746be3da60d6c680a77fd2d20db /nixos/modules | |
parent | 17cf84a0dc0a85c9a77bb6b247cf7ac5210f4458 (diff) | |
parent | 82ef47d3b71c54fdb8449a7a8919a81685572a52 (diff) |
Merge pull request #294096 from WilliButz/repart-image/overridability
systemd-repart: improve overridability of image builder
Diffstat (limited to 'nixos/modules')
-rw-r--r-- | nixos/modules/image/repart-image.nix | 38 | ||||
-rw-r--r-- | nixos/modules/image/repart.nix | 32 |
2 files changed, 59 insertions, 11 deletions
diff --git a/nixos/modules/image/repart-image.nix b/nixos/modules/image/repart-image.nix index 7ac47ee32ff43..5ae523c43f589 100644 --- a/nixos/modules/image/repart-image.nix +++ b/nixos/modules/image/repart-image.nix @@ -3,6 +3,7 @@ { lib , runCommand +, runCommandLocal , python3 , black , ruff @@ -33,6 +34,7 @@ , seed , definitionsDirectory , sectorSize +, mkfsEnv ? {} }: let @@ -50,6 +52,11 @@ let mypy --strict $out ''; + amendedRepartDefinitions = runCommandLocal "amended-repart.d" {} '' + definitions=$(${amendRepartDefinitions} ${partitions} ${definitionsDirectory}) + cp -r $definitions $out + ''; + fileSystemToolMapping = { "vfat" = [ dosfstools mtools ]; "ext4" = [ e2fsprogs.bin ]; @@ -74,28 +81,39 @@ in runCommand imageFileBasename { + __structuredAttrs = true; + nativeBuildInputs = [ systemd fakeroot util-linux compressionPkg ] ++ fileSystemTools; -} '' - amendedRepartDefinitions=$(${amendRepartDefinitions} ${partitions} ${definitionsDirectory}) + env = mkfsEnv; + + systemdRepartFlags = [ + "--dry-run=no" + "--empty=create" + "--size=auto" + "--seed=${seed}" + "--definitions=${amendedRepartDefinitions}" + "--split=${lib.boolToString split}" + "--json=pretty" + ] ++ lib.optionals (sectorSize != null) [ + "--sector-size=${toString sectorSize}" + ]; + + passthru = { + inherit amendRepartDefinitions amendedRepartDefinitions; + }; +} '' mkdir -p $out cd $out echo "Building image with systemd-repart..." unshare --map-root-user fakeroot systemd-repart \ - --dry-run=no \ - --empty=create \ - --size=auto \ - --seed="${seed}" \ - --definitions="$amendedRepartDefinitions" \ - --split="${lib.boolToString split}" \ - --json=pretty \ - ${lib.optionalString (sectorSize != null) "--sector-size=${toString sectorSize}"} \ + ''${systemdRepartFlags[@]} \ ${imageFileBasename}.raw \ | tee repart-output.json diff --git a/nixos/modules/image/repart.nix b/nixos/modules/image/repart.nix index 6a933f0d83ccc..90c9c7e51dfa3 100644 --- a/nixos/modules/image/repart.nix +++ b/nixos/modules/image/repart.nix @@ -60,6 +60,11 @@ let }; }; }; + + mkfsOptionsToEnv = opts: lib.mapAttrs' (fsType: options: { + name = "SYSTEMD_REPART_MKFS_OPTIONS_${lib.toUpper fsType}"; + value = builtins.concatStringsSep " " options; + }) opts; in { options.image.repart = { @@ -183,6 +188,29 @@ in ''; }; + mkfsOptions = lib.mkOption { + type = with lib.types; attrsOf (listOf str); + default = {}; + example = lib.literalExpression '' + { + vfat = [ "-S 512" "-c" ]; + } + ''; + description = lib.mdDoc '' + Specify extra options for created file systems. The specified options + are converted to individual environment variables of the format + `SYSTEMD_REPART_MKFS_OPTIONS_<FSTYPE>`. + + See [upstream systemd documentation](https://github.com/systemd/systemd/blob/v255/docs/ENVIRONMENT.md?plain=1#L575-L577) + for information about the usage of these environment variables. + + The example would produce the following environment variable: + ``` + SYSTEMD_REPART_MKFS_OPTIONS_VFAT="-S 512 -c" + ``` + ''; + }; + }; config = { @@ -239,11 +267,13 @@ in (lib.mapAttrs (_n: v: { Partition = v.repartConfig; }) finalPartitions); partitions = pkgs.writeText "partitions.json" (builtins.toJSON finalPartitions); + + mkfsEnv = mkfsOptionsToEnv cfg.mkfsOptions; in pkgs.callPackage ./repart-image.nix { systemd = cfg.package; inherit (cfg) imageFileBasename compression split seed sectorSize; - inherit fileSystems definitionsDirectory partitions; + inherit fileSystems definitionsDirectory partitions mkfsEnv; }; meta.maintainers = with lib.maintainers; [ nikstur ]; |