about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/image/repart-image.nix
blob: b4a1dfe51ff3a82bfacd787bb5c75bbc47815f32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# This is an expression meant to be called from `./repart.nix`, it is NOT a
# NixOS module that can be imported.

{ lib
, runCommand
, python3
, black
, ruff
, mypy
, systemd
, fakeroot
, util-linux
, dosfstools
, mtools
, e2fsprogs
, squashfsTools
, erofs-utils
, btrfs-progs
, xfsprogs

  # arguments
, name
, fileSystems
, partitions
, split
, seed
, definitionsDirectory
}:

let
  amendRepartDefinitions = runCommand "amend-repart-definitions.py"
    {
      # TODO: ruff does not splice properly in nativeBuildInputs
      depsBuildBuild = [ ruff ];
      nativeBuildInputs = [ python3 black mypy ];
    } ''
    install ${./amend-repart-definitions.py} $out
    patchShebangs --build $out

    black --check --diff $out
    ruff --line-length 88 $out
    mypy --strict $out
  '';

  fileSystemToolMapping = {
    "vfat" = [ dosfstools mtools ];
    "ext4" = [ e2fsprogs.bin ];
    "squashfs" = [ squashfsTools ];
    "erofs" = [ erofs-utils ];
    "btrfs" = [ btrfs-progs ];
    "xfs" = [ xfsprogs ];
  };

  fileSystemTools = builtins.concatMap (f: fileSystemToolMapping."${f}") fileSystems;
in

runCommand name
{
  nativeBuildInputs = [
    systemd
    fakeroot
    util-linux
  ] ++ fileSystemTools;
} ''
  amendedRepartDefinitions=$(${amendRepartDefinitions} ${partitions} ${definitionsDirectory})

  mkdir -p $out
  cd $out

  unshare --map-root-user fakeroot systemd-repart \
    --dry-run=no \
    --empty=create \
    --size=auto \
    --seed="${seed}" \
    --definitions="$amendedRepartDefinitions" \
    --split="${lib.boolToString split}" \
    --json=pretty \
    image.raw \
    | tee repart-output.json
''