diff options
author | David Arnold <david.arnold@iohk.io> | 2022-11-05 15:17:35 -0500 |
---|---|---|
committer | lassulus <lassulus@lassul.us> | 2022-12-02 08:09:37 +0100 |
commit | efc80d7d89d40f54b76e0efcf83578c6b074906e (patch) | |
tree | 28597fea8468438ace7f1b3a1f271a1ddeb6d8d0 | |
parent | aca927667acea7bb01dc8e72ee71f6548d9fd41b (diff) |
types: init swap
-rw-r--r-- | example/swap.nix | 52 | ||||
-rw-r--r-- | module.nix | 1 | ||||
-rw-r--r-- | tests/swap.nix | 19 | ||||
-rw-r--r-- | types.nix | 62 |
4 files changed, 131 insertions, 3 deletions
diff --git a/example/swap.nix b/example/swap.nix new file mode 100644 index 0000000..0693caa --- /dev/null +++ b/example/swap.nix @@ -0,0 +1,52 @@ +{ disks ? [ "/dev/vdb" ], ... }: { + disk = { + vdb = { + device = builtins.elemAt disks 0; + type = "disk"; + content = { + type = "table"; + format = "gpt"; + partitions = [ + { + type = "partition"; + name = "ESP"; + start = "1MiB"; + end = "100MiB"; + bootable = true; + content = { + type = "filesystem"; + format = "vfat"; + mountpoint = "/boot"; + }; + } + { + name = "root"; + type = "partition"; + start = "100MiB"; + end = "-1G"; + part-type = "primary"; + bootable = true; + content = { + type = "filesystem"; + format = "ext4"; + mountpoint = "/"; + }; + } + { + name = "root"; + type = "partition"; + start = "-1G"; + end = "100%"; + part-type = "primary"; + bootable = true; + content = { + type = "swap"; + randomEncryption = true; + }; + } + ]; + }; + }; + }; +} + @@ -42,5 +42,6 @@ in { # Remember to add config keys here if they are added to types fileSystems = lib.mkIf cfg.enableConfig (lib.mkMerge (lib.catAttrs "fileSystems" (types.diskoLib.config cfg.devices))); boot = lib.mkIf cfg.enableConfig (lib.mkMerge (lib.catAttrs "boot" (types.diskoLib.config cfg.devices))); + swapDevices = lib.mkIf cfg.enableConfig (lib.mkMerge (lib.catAttrs "swapDevices" (types.diskoLib.config cfg.devices))); }; } diff --git a/tests/swap.nix b/tests/swap.nix new file mode 100644 index 0000000..fa090f8 --- /dev/null +++ b/tests/swap.nix @@ -0,0 +1,19 @@ +{ pkgs ? (import <nixpkgs> { }) +, makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest +}: +makeDiskoTest { + disko-config = ../example/swap.nix; + extraTestScript = '' + machine.succeed("mountpoint /"); + machine.succeed("swapon --show >&2"); + machine.succeed(""" + lsblk --json | + ${pkgs.jq}/bin/jq -e '.blockdevices[] | + select(.name == "vda") | + .children[] | + select(.name == "vda3") | + .children[0].mountpoints[0] == "[SWAP]" + ' + """); + ''; +} @@ -18,13 +18,13 @@ rec { # option for valid contents of partitions (basically like devices, but without tables) partitionType = mkOption { - type = types.nullOr (diskoLib.subType { inherit btrfs filesystem zfs mdraid luks lvm_pv; }); + type = types.nullOr (diskoLib.subType { inherit btrfs filesystem zfs mdraid luks lvm_pv swap; }); default = null; }; # option for valid contents of devices deviceType = mkOption { - type = types.nullOr (diskoLib.subType { inherit table btrfs filesystem zfs mdraid luks lvm_pv; }); + type = types.nullOr (diskoLib.subType { inherit table btrfs filesystem zfs mdraid luks lvm_pv swap; }); default = null; }; @@ -547,6 +547,62 @@ rec { }; }); + swap = types.submodule ({ config, ... }: { + options = { + type = mkOption { + type = types.enum [ "swap" ]; + internal = true; + }; + randomEncryption = mkOption { + type = types.bool; + default = false; + }; + _meta = mkOption { + internal = true; + readOnly = true; + type = types.functionTo diskoLib.jsonType; + default = dev: { + }; + }; + _create = mkOption { + internal = true; + readOnly = true; + type = types.functionTo types.str; + default = dev: '' + mkswap ${dev} + ''; + }; + _mount = mkOption { + internal = true; + readOnly = true; + type = types.functionTo diskoLib.jsonType; + default = dev: { + fs.${dev} = '' + if ! $(swapon --show | grep -q '^${dev} '); then + swapon ${dev} + fi + ''; + }; + }; + _config = mkOption { + internal = true; + readOnly = true; + default = dev: [{ + swapDevices = [{ + device = dev; + randomEncryption = config.randomEncryption; + }]; + }]; + }; + _pkgs = mkOption { + internal = true; + readOnly = true; + type = types.functionTo (types.listOf types.package); + default = pkgs: [ pkgs.util-linux ]; + }; + }; + }); + lvm_pv = types.submodule ({ config, ... }: { options = { type = mkOption { @@ -1158,7 +1214,7 @@ rec { type = types.enum [ "disk" ]; }; device = mkOption { - type = optionTypes.absolute-pathname; # TODO check if subpath of /dev ? + type = optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile }; content = diskoLib.deviceType; _meta = mkOption { |