diff options
-rw-r--r-- | default.nix | 14 | ||||
-rw-r--r-- | example/btrfs-subvolumes.nix | 26 | ||||
-rw-r--r-- | tests/btrfs-subvolumes.nix | 40 |
3 files changed, 80 insertions, 0 deletions
diff --git a/default.nix b/default.nix index 19333fc..c6ec47c 100644 --- a/default.nix +++ b/default.nix @@ -69,6 +69,18 @@ let create-f = q: x: create.${x.type} q x; + create.btrfs = q: x: '' + mkfs.btrfs ${q.device} + ${lib.optionalString (!isNull x.subvolumes or null) '' + MNTPOINT=$(mktemp -d) + ( + mount ${q.device} "$MNTPOINT" + trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT + ${concatMapStringsSep "\n" (subvolume: "btrfs subvolume create \"$MNTPOINT\"/${subvolume}") x.subvolumes} + ) + ''} + ''; + create.filesystem = q: x: '' mkfs.${x.format} ${q.device} ''; @@ -178,6 +190,8 @@ let ''; }; + mount.btrfs = mount.filesystem; + mount.devices = q: x: let z = foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { device = "/dev/${name}"; inherit name; }) x.content); # attrValues returns values sorted by name. This is important, because it diff --git a/example/btrfs-subvolumes.nix b/example/btrfs-subvolumes.nix new file mode 100644 index 0000000..0124c86 --- /dev/null +++ b/example/btrfs-subvolumes.nix @@ -0,0 +1,26 @@ +{ + type = "devices"; + content = { + vdb = { + type = "table"; + format = "gpt"; + partitions = [ + { + type = "partition"; + part-type = "primary"; + start = "0%"; + end = "100%"; + content = { + type = "btrfs"; + mountpoint = "/"; + subvolumes = [ + "/home" + "/test" + ]; + }; + } + ]; + }; + }; +} + diff --git a/tests/btrfs-subvolumes.nix b/tests/btrfs-subvolumes.nix new file mode 100644 index 0000000..6527329 --- /dev/null +++ b/tests/btrfs-subvolumes.nix @@ -0,0 +1,40 @@ +{ makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix> +, pkgs ? (import <nixpkgs> {}) +}: +let + makeTest' = args: + makeTest args { + inherit pkgs; + inherit (pkgs) system; + }; + disko-config = import ../example/btrfs-subvolumes.nix; + tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); + tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); +in makeTest' { + name = "disko"; + + nodes.machine = + { config, pkgs, modulesPath, ... }: + + { + imports = [ + (modulesPath + "/profiles/installation-device.nix") + (modulesPath + "/profiles/base.nix") + ]; + + # speed-up eval + documentation.enable = false; + + virtualisation.emptyDiskImages = [ 512 ]; + }; + + testScript = '' + machine.succeed("echo 'secret' > /tmp/secret.key"); + machine.succeed("${tsp-create}"); + machine.succeed("${tsp-mount}"); + machine.succeed("${tsp-mount}"); # verify that the command is idempotent + machine.succeed("test -e /mnt/test"); + machine.succeed("btrfs subvolume list /mnt | grep -qs 'path test$'"); + ''; +} + |