fix inconsistent indentation with nixpkgs-fmt

This commit is contained in:
Jörg Thalheim 2022-08-26 08:41:58 +02:00
parent 1ac0e76b15
commit 69f1337980
8 changed files with 138 additions and 122 deletions

View file

@ -4,12 +4,13 @@ with builtins;
let let
helper.find-device = device: let helper.find-device = device:
environment = helper.device-id device; let
in environment = helper.device-id device;
in
# DEVICE points already to /dev/disk, so we don't handle it via /dev/disk/by-path # DEVICE points already to /dev/disk, so we don't handle it via /dev/disk/by-path
if hasPrefix "/dev/disk" device then if hasPrefix "/dev/disk" device then
"${environment}='${device}'" "${environment}='${device}'"
else '' else ''
${environment}=$(for x in $(find /dev/disk/{by-path,by-id}/); do ${environment}=$(for x in $(find /dev/disk/{by-path,by-id}/); do
dev=$x dev=$x
@ -46,7 +47,7 @@ let
}; };
config.devices = q: x: config.devices = q: x:
foldl' recursiveUpdate {} (mapAttrsToList (name: config-f { device = "/dev/${name}"; }) x.content); foldl' recursiveUpdate { } (mapAttrsToList (name: config-f { device = "/dev/${name}"; }) x.content);
config.luks = q: x: { config.luks = q: x: {
boot.initrd.luks.devices.${x.name}.device = q.device; boot.initrd.luks.devices.${x.name}.device = q.device;
@ -56,15 +57,15 @@ let
config-f { device = "/dev/${q.vgname}/${q.name}"; } x.content; config-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
config.lvm_vg = q: x: config.lvm_vg = q: x:
foldl' recursiveUpdate {} (mapAttrsToList (name: config-f { inherit name; vgname = x.name; }) x.lvs); foldl' recursiveUpdate { } (mapAttrsToList (name: config-f { inherit name; vgname = x.name; }) x.lvs);
config.noop = q: x: {}; config.noop = q: x: { };
config.partition = q: x: config.partition = q: x:
config-f { device = q.device + toString q.index; } x.content; config-f { device = q.device + toString q.index; } x.content;
config.table = q: x: config.table = q: x:
foldl' recursiveUpdate {} (imap (index: config-f (q // { inherit index; })) x.partitions); foldl' recursiveUpdate { } (imap (index: config-f (q // { inherit index; })) x.partitions);
create-f = q: x: create.${x.type} q x; create-f = q: x: create.${x.type} q x;
@ -87,13 +88,15 @@ let
${q.device} ${q.device}
''; '';
create.devices = q: x: let create.devices = q: x:
raid-devices = lib.filterAttrs (_: dev: dev.type == "mdadm" || dev.type == "zpool" || dev.type == "lvm_vg") x.content; let
other-devices = lib.filterAttrs (_: dev: dev.type != "mdadm" && dev.type != "zpool" && dev.type != "lvm_vg") x.content; raid-devices = lib.filterAttrs (_: dev: dev.type == "mdadm" || dev.type == "zpool" || dev.type == "lvm_vg") x.content;
in '' other-devices = lib.filterAttrs (_: dev: dev.type != "mdadm" && dev.type != "zpool" && dev.type != "lvm_vg") x.content;
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; }) other-devices)} in
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; name = name; }) raid-devices)} ''
''; ${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; }) other-devices)}
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; name = name; }) raid-devices)}
'';
create.mdraid = q: x: '' create.mdraid = q: x: ''
RAIDDEVICES_N_${x.name}=$((''${RAIDDEVICES_N_${x.name}:-0}+1)) RAIDDEVICES_N_${x.name}=$((''${RAIDDEVICES_N_${x.name}:-0}+1))
@ -134,20 +137,22 @@ let
create.noop = q: x: ""; create.noop = q: x: "";
create.partition = q: x: let create.partition = q: x:
env = helper.device-id q.device; let
in '' env = helper.device-id q.device;
parted -s "''${${env}}" mkpart ${x.part-type} ${x.fs-type or ""} ${x.start} ${x.end} in
# ensure /dev/disk/by-path/..-partN exists before continuing ''
udevadm trigger --subsystem-match=block; udevadm settle parted -s "''${${env}}" mkpart ${x.part-type} ${x.fs-type or ""} ${x.start} ${x.end}
${optionalString (x.bootable or false) '' # ensure /dev/disk/by-path/..-partN exists before continuing
parted -s "''${${env}}" set ${toString q.index} boot on udevadm trigger --subsystem-match=block; udevadm settle
''} ${optionalString (x.bootable or false) ''
${concatMapStringsSep "" (flag: '' parted -s "''${${env}}" set ${toString q.index} boot on
parted -s "''${${env}}" set ${toString q.index} ${flag} on ''}
'') (x.flags or [])} ${concatMapStringsSep "" (flag: ''
${create-f { device = "\"\${${env}}\"-part" + toString q.index; } x.content} parted -s "''${${env}}" set ${toString q.index} ${flag} on
''; '') (x.flags or [])}
${create-f { device = "\"\${${env}}\"-part" + toString q.index; } x.content}
'';
create.table = q: x: '' create.table = q: x: ''
${helper.find-device q.device} ${helper.find-device q.device}
@ -185,34 +190,38 @@ let
mount-f = q: x: mount.${x.type} q x; mount-f = q: x: mount.${x.type} q x;
mount.filesystem = q: x: { mount.filesystem = q: x: {
fs.${x.mountpoint} = '' fs.${x.mountpoint} = ''
if ! findmnt ${q.device} "/mnt${x.mountpoint}" > /dev/null 2>&1; then if ! findmnt ${q.device} "/mnt${x.mountpoint}" > /dev/null 2>&1; then
mount ${q.device} "/mnt${x.mountpoint}" -o X-mount.mkdir mount ${q.device} "/mnt${x.mountpoint}" -o X-mount.mkdir
fi fi
''; '';
}; };
mount.btrfs = mount.filesystem; mount.btrfs = mount.filesystem;
mount.devices = q: x: let mount.devices = q: x:
z = foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { device = "/dev/${name}"; inherit name; }) x.content); let
# attrValues returns values sorted by name. This is important, because it z = foldl' recursiveUpdate { } (mapAttrsToList (name: mount-f { device = "/dev/${name}"; inherit name; }) x.content);
# ensures that "/" is processed before "/foo" etc. # attrValues returns values sorted by name. This is important, because it
in '' # ensures that "/" is processed before "/foo" etc.
${optionalString (hasAttr "table" z) (concatStringsSep "\n" (attrValues z.table))} in
${optionalString (hasAttr "luks" z) (concatStringsSep "\n" (attrValues z.luks))} ''
${optionalString (hasAttr "lvm" z) (concatStringsSep "\n" (attrValues z.lvm))} ${optionalString (hasAttr "table" z) (concatStringsSep "\n" (attrValues z.table))}
${optionalString (hasAttr "zpool" z) (concatStringsSep "\n" (attrValues z.zpool))} ${optionalString (hasAttr "luks" z) (concatStringsSep "\n" (attrValues z.luks))}
${optionalString (hasAttr "zfs" z) (concatStringsSep "\n" (attrValues z.zfs))} ${optionalString (hasAttr "lvm" z) (concatStringsSep "\n" (attrValues z.lvm))}
${optionalString (hasAttr "fs" z) (concatStringsSep "\n" (attrValues z.fs))} ${optionalString (hasAttr "zpool" z) (concatStringsSep "\n" (attrValues z.zpool))}
''; ${optionalString (hasAttr "zfs" z) (concatStringsSep "\n" (attrValues z.zfs))}
${optionalString (hasAttr "fs" z) (concatStringsSep "\n" (attrValues z.fs))}
'';
mount.luks = q: x: ( mount.luks = q: x: (
recursiveUpdate recursiveUpdate
(mount-f { device = "/dev/mapper/${x.name}"; } x.content) (mount-f { device = "/dev/mapper/${x.name}"; } x.content)
{luks.${q.device} = '' {
cryptsetup status ${x.name} >/dev/null 2>/dev/null || cryptsetup luksOpen ${q.device} ${x.name} ${if builtins.hasAttr "keyfile" x then "--key-file " + x.keyfile else ""} luks.${q.device} = ''
'';} cryptsetup status ${x.name} >/dev/null 2>/dev/null || cryptsetup luksOpen ${q.device} ${x.name} ${if builtins.hasAttr "keyfile" x then "--key-file " + x.keyfile else ""}
'';
}
); );
mount.lvm_lv = q: x: mount.lvm_lv = q: x:
@ -220,15 +229,17 @@ let
mount.lvm_vg = q: x: ( mount.lvm_vg = q: x: (
recursiveUpdate recursiveUpdate
(foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { inherit name; vgname = q.name; }) x.lvs)) (foldl' recursiveUpdate { } (mapAttrsToList (name: mount-f { inherit name; vgname = q.name; }) x.lvs))
{lvm.${q.device} = '' {
vgchange -a y lvm.${q.device} = ''
'';} vgchange -a y
'';
}
); );
mount.lvm_pv = mount.noop; mount.lvm_pv = mount.noop;
mount.noop = q: x: {}; mount.noop = q: x: { };
mount.mdadm = q: x: mount.mdadm = q: x:
mount-f { device = "/dev/md/${q.name}"; } x.content; mount-f { device = "/dev/md/${q.name}"; } x.content;
@ -239,36 +250,39 @@ let
mount.table = q: x: ( mount.table = q: x: (
recursiveUpdate recursiveUpdate
(foldl' recursiveUpdate {} (imap (index: mount-f (q // { inherit index; device = helper.device-id q.device; })) x.partitions)) (foldl' recursiveUpdate { } (imap (index: mount-f (q // { inherit index; device = helper.device-id q.device; })) x.partitions))
{table.${q.device} = helper.find-device q.device;} { table.${q.device} = helper.find-device q.device; }
); );
mount.zfs = mount.noop; mount.zfs = mount.noop;
mount.zpool = q: x: ( mount.zpool = q: x: (
recursiveUpdate recursiveUpdate
(foldl' recursiveUpdate {} (map (mount-f (q // { pool = q.name; })) x.datasets)) (foldl' recursiveUpdate { } (map (mount-f (q // { pool = q.name; })) x.datasets))
{zpool.${q.device} = '' {
zpool list '${q.name}' >/dev/null 2>/dev/null || zpool import '${q.name}' zpool.${q.device} = ''
'';} zpool list '${q.name}' >/dev/null 2>/dev/null || zpool import '${q.name}'
'';
}
); );
mount.zfs_filesystem = q: x: { mount.zfs_filesystem = q: x: {
zfs.${x.mountpoint} = '' zfs.${x.mountpoint} = ''
if ! findmnt '${q.pool}/${x.name}' /mnt${x.mountpoint} > /dev/null 2>&1; then if ! findmnt '${q.pool}/${x.name}' /mnt${x.mountpoint} > /dev/null 2>&1; then
mount \ mount \
${lib.optionalString ((x.options.mountpoint or "") != "legacy") "-o zfsutil"} \ ${lib.optionalString ((x.options.mountpoint or "") != "legacy") "-o zfsutil"} \
-t zfs ${q.pool}/${x.name} /mnt${x.mountpoint} \ -t zfs ${q.pool}/${x.name} /mnt${x.mountpoint} \
-o X-mount.mkdir -o X-mount.mkdir
fi fi
''; '';
}; };
mount.zfs_volume = q: x: mount.zfs_volume = q: x:
mount-f { device = "/dev/zvol/${q.pool}/${x.name}"; } x.content; mount-f { device = "/dev/zvol/${q.pool}/${x.name}"; } x.content;
in { in
config = config-f {}; {
config = config-f { };
create = cfg: '' create = cfg: ''
set -efux set -efux
${create-f {} cfg} ${create-f {} cfg}

View file

@ -1,5 +1,5 @@
{ pkgs ? (import <nixpkgs> {}) { pkgs ? (import <nixpkgs> { })
, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest , makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
}: }:
makeDiskoTest { makeDiskoTest {
disko-config = import ../example/btrfs-subvolumes.nix; disko-config = import ../example/btrfs-subvolumes.nix;

View file

@ -1,5 +1,5 @@
{ makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix> { makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, pkgs ? (import <nixpkgs> {}) , pkgs ? (import <nixpkgs> { })
}@args: }@args:
let let
lib = pkgs.lib; lib = pkgs.lib;
@ -12,4 +12,5 @@ let
); );
allTests = lib.genAttrs (allTestFilenames) (test: import (./. + "/${test}.nix") { inherit makeDiskoTest; }); allTests = lib.genAttrs (allTestFilenames) (test: import (./. + "/${test}.nix") { inherit makeDiskoTest; });
in allTests in
allTests

View file

@ -1,47 +1,48 @@
{ pkgs ? (import <nixpkgs> {}) { pkgs ? (import <nixpkgs> { })
, makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix> , makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, ... , ...
}: }:
{ {
makeDiskoTest = { makeDiskoTest =
disko-config, { disko-config
extraTestScript, , extraTestScript
extraConfig ? {} , extraConfig ? { }
}: }:
let let
lib = pkgs.lib; lib = pkgs.lib;
makeTest' = args: makeTest' = args:
makeTest args { makeTest args {
inherit pkgs; inherit pkgs;
inherit (pkgs) system; inherit (pkgs) system;
}; };
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create disko-config);
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount disko-config);
num-disks = builtins.length (builtins.filter (x: builtins.match "vd." x == []) (lib.attrNames disko-config.content)); num-disks = builtins.length (builtins.filter (x: builtins.match "vd." x == [ ]) (lib.attrNames disko-config.content));
in makeTest' { in
name = "disko"; makeTest' {
name = "disko";
nodes.machine = nodes.machine =
{ config, pkgs, modulesPath, ... }: { config, pkgs, modulesPath, ... }:
{ {
imports = [ imports = [
(modulesPath + "/profiles/installation-device.nix") (modulesPath + "/profiles/installation-device.nix")
(modulesPath + "/profiles/base.nix") (modulesPath + "/profiles/base.nix")
]; ];
# speed-up eval # speed-up eval
documentation.enable = false; documentation.enable = false;
virtualisation.emptyDiskImages = builtins.genList (_: 512) num-disks; virtualisation.emptyDiskImages = builtins.genList (_: 512) num-disks;
} // extraConfig; } // extraConfig;
testScript = '' testScript = ''
machine.succeed("echo 'secret' > /tmp/secret.key"); machine.succeed("echo 'secret' > /tmp/secret.key");
machine.succeed("${tsp-create}"); machine.succeed("${tsp-create}");
machine.succeed("${tsp-mount}"); machine.succeed("${tsp-mount}");
machine.succeed("${tsp-mount}"); # verify that the command is idempotent machine.succeed("${tsp-mount}"); # verify that the command is idempotent
${extraTestScript} ${extraTestScript}
''; '';
}; };
} }

View file

@ -1,5 +1,5 @@
{ pkgs ? (import <nixpkgs> {}) { pkgs ? (import <nixpkgs> { })
, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest , makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
}: }:
makeDiskoTest { makeDiskoTest {
disko-config = import ../example/luks-lvm.nix; disko-config = import ../example/luks-lvm.nix;

View file

@ -1,5 +1,5 @@
{ pkgs ? (import <nixpkgs> {}) { pkgs ? (import <nixpkgs> { })
, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest , makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
}: }:
makeDiskoTest { makeDiskoTest {
disko-config = import ../example/lvm-raid.nix; disko-config = import ../example/lvm-raid.nix;

View file

@ -1,5 +1,5 @@
{ pkgs ? (import <nixpkgs> {}) { pkgs ? (import <nixpkgs> { })
, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest , makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
}: }:
makeDiskoTest { makeDiskoTest {
disko-config = import ../example/mdadm.nix; disko-config = import ../example/mdadm.nix;

View file

@ -1,5 +1,5 @@
{ pkgs ? (import <nixpkgs> {}) { pkgs ? (import <nixpkgs> { })
, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest , makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
}: }:
makeDiskoTest { makeDiskoTest {
disko-config = import ../example/zfs.nix; disko-config = import ../example/zfs.nix;