2022-09-13 11:21:37 +00:00
|
|
|
|
{ lib }:
|
2022-09-04 11:09:10 +00:00
|
|
|
|
with lib;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
with builtins;
|
|
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
|
|
|
|
diskoLib = {
|
|
|
|
|
# like types.oneOf but instead of a list takes an attrset
|
|
|
|
|
# uses the field "type" to find the correct type in the attrset
|
|
|
|
|
subType = typeAttr: mkOptionType rec {
|
|
|
|
|
name = "subType";
|
2022-12-31 20:30:39 +00:00
|
|
|
|
description = "one of ${concatStringsSep "," (attrNames typeAttr)}";
|
2022-09-04 09:46:37 +00:00
|
|
|
|
check = x: if x ? type then typeAttr.${x.type}.check x else throw "No type option set in:\n${generators.toPretty {} x}";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
merge = loc: defs:
|
|
|
|
|
foldl' (res: def: typeAttr.${def.value.type}.merge loc [def]) {} defs;
|
|
|
|
|
nestedTypes = typeAttr;
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-21 11:00:45 +00:00
|
|
|
|
# option for valid contents of partitions (basically like devices, but without tables)
|
2022-08-29 09:45:19 +00:00
|
|
|
|
partitionType = mkOption {
|
2022-11-05 20:17:35 +00:00
|
|
|
|
type = types.nullOr (diskoLib.subType { inherit btrfs filesystem zfs mdraid luks lvm_pv swap; });
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "The type of partition";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# option for valid contents of devices
|
|
|
|
|
deviceType = mkOption {
|
2022-11-05 20:17:35 +00:00
|
|
|
|
type = types.nullOr (diskoLib.subType { inherit table btrfs filesystem zfs mdraid luks lvm_pv swap; });
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "The type of device";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* deepMergeMap takes a function and a list of attrsets and deep merges them
|
|
|
|
|
|
|
|
|
|
deepMergeMap :: -> (AttrSet -> AttrSet ) -> [ AttrSet ] -> Attrset
|
|
|
|
|
|
|
|
|
|
Example:
|
2022-10-01 19:29:36 +00:00
|
|
|
|
deepMergeMap (x: x.t = "test") [ { x = { y = 1; z = 3; }; } { x = { bla = 234; }; } ]
|
|
|
|
|
=> { x = { y = 1; z = 3; bla = 234; t = "test"; }; }
|
2022-08-29 09:45:19 +00:00
|
|
|
|
*/
|
|
|
|
|
deepMergeMap = f: listOfAttrs:
|
|
|
|
|
foldr (attr: acc: (recursiveUpdate acc (f attr))) {} listOfAttrs;
|
|
|
|
|
|
|
|
|
|
/* get a device and an index to get the matching device name
|
|
|
|
|
|
|
|
|
|
deviceNumbering :: str -> int -> str
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
deviceNumbering "/dev/sda" 3
|
|
|
|
|
=> "/dev/sda3"
|
|
|
|
|
|
|
|
|
|
deviceNumbering "/dev/disk/by-id/xxx" 2
|
|
|
|
|
=> "/dev/disk/by-id/xxx-part2"
|
|
|
|
|
*/
|
|
|
|
|
deviceNumbering = dev: index:
|
2022-11-15 18:30:09 +00:00
|
|
|
|
if match "/dev/[vs]d.+" dev != null then
|
2022-11-15 18:28:28 +00:00
|
|
|
|
dev + toString index # /dev/{s,v}da style
|
2022-11-15 18:30:09 +00:00
|
|
|
|
else if match "/dev/disk/.+" dev != null then
|
2022-11-15 18:28:28 +00:00
|
|
|
|
"${dev}-part${toString index}" # /dev/disk/by-id/xxx style
|
2022-11-15 18:30:09 +00:00
|
|
|
|
else if match "/dev/(nvme|md/|mmcblk).+" dev != null then
|
2022-11-15 18:28:28 +00:00
|
|
|
|
"${dev}p${toString index}" # /dev/nvme0n1p1 style
|
|
|
|
|
else
|
|
|
|
|
abort "${dev} seems not to be a supported disk format";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2022-09-13 11:21:37 +00:00
|
|
|
|
/* A nix option type representing a json datastructure, vendored from nixpkgs to avoid dependency on pkgs */
|
|
|
|
|
jsonType = let
|
|
|
|
|
valueType = types.nullOr (types.oneOf [
|
|
|
|
|
types.bool
|
|
|
|
|
types.int
|
|
|
|
|
types.float
|
|
|
|
|
types.str
|
|
|
|
|
types.path
|
|
|
|
|
(types.attrsOf valueType)
|
|
|
|
|
(types.listOf valueType)
|
|
|
|
|
]) // {
|
|
|
|
|
description = "JSON value";
|
|
|
|
|
};
|
|
|
|
|
in valueType;
|
|
|
|
|
|
2022-10-21 12:44:17 +00:00
|
|
|
|
/* Given a attrset of deviceDependencies and a devices attrset
|
|
|
|
|
returns a sorted list by deviceDependencies. aborts if a loop is found
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
|
|
|
|
sortDevicesByDependencies :: AttrSet -> AttrSet -> [ [ str str ] ]
|
|
|
|
|
*/
|
2022-10-21 12:44:17 +00:00
|
|
|
|
sortDevicesByDependencies = deviceDependencies: devices:
|
2022-08-29 09:45:19 +00:00
|
|
|
|
let
|
|
|
|
|
dependsOn = a: b:
|
2022-10-21 12:44:17 +00:00
|
|
|
|
elem a (attrByPath b [] deviceDependencies);
|
2022-08-29 09:45:19 +00:00
|
|
|
|
maybeSortedDevices = toposort dependsOn (diskoLib.deviceList devices);
|
|
|
|
|
in
|
|
|
|
|
if (hasAttr "cycle" maybeSortedDevices) then
|
|
|
|
|
abort "detected a cycle in your disk setup: ${maybeSortedDevices.cycle}"
|
|
|
|
|
else
|
|
|
|
|
maybeSortedDevices.result;
|
|
|
|
|
|
|
|
|
|
/* Takes a devices attrSet and returns it as a list
|
|
|
|
|
|
|
|
|
|
deviceList :: AttrSet -> [ [ str str ] ]
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
deviceList { zfs.pool1 = {}; zfs.pool2 = {}; mdadm.raid1 = {}; }
|
|
|
|
|
=> [ [ "zfs" "pool1" ] [ "zfs" "pool2" ] [ "mdadm" "raid1" ] ]
|
|
|
|
|
*/
|
|
|
|
|
deviceList = devices:
|
|
|
|
|
concatLists (mapAttrsToList (n: v: (map (x: [ n x ]) (attrNames v))) devices);
|
|
|
|
|
|
|
|
|
|
/* Takes either a string or null and returns the string or an empty string
|
|
|
|
|
|
|
|
|
|
maybeStr :: Either (str null) -> str
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
maybeStr null
|
|
|
|
|
=> ""
|
|
|
|
|
maybeSTr "hello world"
|
|
|
|
|
=> "hello world"
|
|
|
|
|
*/
|
|
|
|
|
maybeStr = x: optionalString (!isNull x) x;
|
2022-10-22 21:35:16 +00:00
|
|
|
|
|
2023-01-06 14:58:32 +00:00
|
|
|
|
/* Takes a Submodules config and options argument and returns a serializable
|
|
|
|
|
subset of config variables as a shell script snippet.
|
|
|
|
|
*/
|
|
|
|
|
defineHookVariables = {config, options}:
|
2023-01-10 09:45:57 +00:00
|
|
|
|
let
|
|
|
|
|
sanitizeName = lib.replaceStrings ["-"] ["_"];
|
|
|
|
|
isAttrsOfSubmodule = o: o.type.name == "attrsOf" && o.type.nestedTypes.elemType.name == "submodule";
|
|
|
|
|
isSerializable = n: o: !(
|
|
|
|
|
lib.hasPrefix "_" n
|
|
|
|
|
|| lib.hasSuffix "Hook" n
|
|
|
|
|
|| isAttrsOfSubmodule o
|
|
|
|
|
# TODO don't hardcode diskoLib.subType options.
|
|
|
|
|
|| n == "content" || n == "partitions");
|
|
|
|
|
in
|
2023-01-06 14:58:32 +00:00
|
|
|
|
lib.toShellVars
|
2023-01-10 09:45:57 +00:00
|
|
|
|
(lib.mapAttrs'
|
|
|
|
|
(n: o: lib.nameValuePair (sanitizeName n) o.value)
|
|
|
|
|
(lib.filterAttrs isSerializable options));
|
2023-01-06 14:58:32 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
hookMixin = { config, options,... }: {
|
|
|
|
|
options = let
|
2023-01-14 16:24:49 +00:00
|
|
|
|
mkHook = default: mkOption {
|
2023-01-06 18:59:47 +00:00
|
|
|
|
type = types.str;
|
2023-01-14 16:24:49 +00:00
|
|
|
|
default = default;
|
2023-01-06 18:59:47 +00:00
|
|
|
|
};
|
|
|
|
|
in {
|
2023-01-14 16:24:49 +00:00
|
|
|
|
preCreateHook = mkHook "echo 'DEBUG preCreate: ${config.type}'";
|
|
|
|
|
postCreateHook = mkHook "echo 'DEBUG postCreate: ${config.type}'";
|
|
|
|
|
preMountHook = mkHook "echo 'DEBUG preMount: ${config.type}'";
|
|
|
|
|
postMountHook = mkHook "echo 'DEBUG postMount: ${config.type}'";
|
2023-01-06 18:59:47 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-10 15:36:13 +00:00
|
|
|
|
mkCreateOption = { config, options, default }@attrs:
|
2023-01-10 09:25:21 +00:00
|
|
|
|
mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo types.str;
|
2023-01-10 09:45:57 +00:00
|
|
|
|
default = args:
|
2023-01-14 16:24:49 +00:00
|
|
|
|
let name = "format";
|
|
|
|
|
test = lib.optionalString (config ? name) "${config.${name}}";
|
|
|
|
|
in
|
|
|
|
|
''
|
2023-01-15 11:51:23 +00:00
|
|
|
|
( # ${config.type} ${concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]}
|
2023-01-14 16:24:49 +00:00
|
|
|
|
${diskoLib.defineHookVariables { inherit config options; }}
|
|
|
|
|
${config.preCreateHook}
|
|
|
|
|
${attrs.default args}
|
|
|
|
|
${config.postCreateHook}
|
2023-01-15 11:51:23 +00:00
|
|
|
|
)
|
2023-01-14 16:24:49 +00:00
|
|
|
|
'';
|
2023-01-10 09:25:21 +00:00
|
|
|
|
description = "Creation script";
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-10 15:36:13 +00:00
|
|
|
|
mkMountOption = { config, options, default }@attrs:
|
|
|
|
|
mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
|
|
|
|
default = args: let v = attrs.default args; in lib.traceSeqN 3 [(config.name or "unnamed") (v.fs or "")] v;
|
|
|
|
|
description = "Mount script";
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-10 09:25:21 +00:00
|
|
|
|
|
2022-10-22 21:35:16 +00:00
|
|
|
|
/* Takes a disko device specification, returns an attrset with metadata
|
|
|
|
|
|
|
|
|
|
meta :: types.devices -> AttrSet
|
|
|
|
|
*/
|
|
|
|
|
meta = devices: diskoLib.deepMergeMap (dev: dev._meta) (flatten (map attrValues (attrValues devices)));
|
2022-12-03 11:08:19 +00:00
|
|
|
|
|
2022-10-22 21:35:16 +00:00
|
|
|
|
/* Takes a disko device specification and returns a string which formats the disks
|
|
|
|
|
|
|
|
|
|
create :: types.devices -> str
|
|
|
|
|
*/
|
|
|
|
|
create = devices: let
|
2022-11-10 08:36:31 +00:00
|
|
|
|
sortedDeviceList = diskoLib.sortDevicesByDependencies ((diskoLib.meta devices).deviceDependencies or {}) devices;
|
2022-10-22 21:35:16 +00:00
|
|
|
|
in ''
|
|
|
|
|
set -efux
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${concatMapStrings (dev: (attrByPath (dev ++ [ "_create" ]) ({}: {}) devices) {}) sortedDeviceList}
|
2022-10-22 21:35:16 +00:00
|
|
|
|
'';
|
|
|
|
|
/* Takes a disko device specification and returns a string which mounts the disks
|
|
|
|
|
|
|
|
|
|
mount :: types.devices -> str
|
|
|
|
|
*/
|
|
|
|
|
mount = devices: let
|
2023-01-09 19:35:54 +00:00
|
|
|
|
fsMounts = diskoLib.deepMergeMap (dev: (dev._mount {}).fs or {}) (flatten (map attrValues (attrValues devices)));
|
2022-11-10 08:36:31 +00:00
|
|
|
|
sortedDeviceList = diskoLib.sortDevicesByDependencies ((diskoLib.meta devices).deviceDependencies or {}) devices;
|
2022-10-22 21:35:16 +00:00
|
|
|
|
in ''
|
|
|
|
|
set -efux
|
2022-11-21 11:00:45 +00:00
|
|
|
|
# first create the necessary devices
|
2023-01-09 19:35:54 +00:00
|
|
|
|
${concatMapStrings (dev: ((attrByPath (dev ++ [ "_mount" ]) "" devices) {}).dev ) sortedDeviceList}
|
2022-10-22 21:35:16 +00:00
|
|
|
|
|
|
|
|
|
# and then mount the filesystems in alphabetical order
|
|
|
|
|
${concatStrings (attrValues fsMounts)}
|
|
|
|
|
'';
|
2022-12-08 20:03:11 +00:00
|
|
|
|
|
2022-11-23 12:28:52 +00:00
|
|
|
|
/* takes a disko device specification and returns a string which unmounts, destroys all disks and then runs create and mount
|
|
|
|
|
|
|
|
|
|
zapCreateMount :: types.devices -> str
|
|
|
|
|
*/
|
|
|
|
|
zapCreateMount = devices: ''
|
|
|
|
|
set -efux
|
2022-12-01 14:39:11 +00:00
|
|
|
|
umount -Rv /mnt || :
|
2022-11-23 12:28:52 +00:00
|
|
|
|
|
2022-12-01 14:39:11 +00:00
|
|
|
|
for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do
|
|
|
|
|
${./disk-deactivate}/disk-deactivate "$dev" | bash -x
|
|
|
|
|
done
|
2022-11-25 11:52:40 +00:00
|
|
|
|
|
2022-11-23 12:28:52 +00:00
|
|
|
|
echo 'creating partitions...'
|
|
|
|
|
${diskoLib.create devices}
|
|
|
|
|
echo 'mounting partitions...'
|
|
|
|
|
${diskoLib.mount devices}
|
|
|
|
|
'';
|
2022-10-22 21:35:16 +00:00
|
|
|
|
/* Takes a disko device specification and returns a nixos configuration
|
|
|
|
|
|
|
|
|
|
config :: types.devices -> nixosConfig
|
|
|
|
|
*/
|
2022-10-23 09:30:08 +00:00
|
|
|
|
config = devices: flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))));
|
2022-10-22 21:35:16 +00:00
|
|
|
|
/* Takes a disko device specification and returns a function to get the needed packages to format/mount the disks
|
|
|
|
|
|
|
|
|
|
packages :: types.devices -> pkgs -> [ derivation ]
|
|
|
|
|
*/
|
|
|
|
|
packages = devices: pkgs: unique (flatten (map (dev: dev._pkgs pkgs) (flatten (map attrValues (attrValues devices)))));
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
optionTypes = rec {
|
|
|
|
|
# POSIX.1‐2017, 3.281 Portable Filename
|
|
|
|
|
filename = mkOptionType {
|
|
|
|
|
name = "POSIX portable filename";
|
|
|
|
|
check = x: isString x && builtins.match "[0-9A-Za-z._][0-9A-Za-z._-]*" x != null;
|
|
|
|
|
merge = mergeOneOption;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "A filename";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# POSIX.1‐2017, 3.2 Absolute Pathname
|
|
|
|
|
absolute-pathname = mkOptionType {
|
|
|
|
|
name = "POSIX absolute pathname";
|
|
|
|
|
check = x: isString x && substring 0 1 x == "/" && pathname.check x;
|
|
|
|
|
merge = mergeOneOption;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "An absolute path";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# POSIX.1-2017, 3.271 Pathname
|
|
|
|
|
pathname = mkOptionType {
|
|
|
|
|
name = "POSIX pathname";
|
|
|
|
|
check = x:
|
|
|
|
|
let
|
|
|
|
|
# The filter is used to normalize paths, i.e. to remove duplicated and
|
|
|
|
|
# trailing slashes. It also removes leading slashes, thus we have to
|
|
|
|
|
# check for "/" explicitly below.
|
|
|
|
|
xs = filter (s: stringLength s > 0) (splitString "/" x);
|
|
|
|
|
in
|
|
|
|
|
isString x && (x == "/" || (length xs > 0 && all filename.check xs));
|
|
|
|
|
merge = mergeOneOption;
|
2023-01-03 23:35:42 +00:00
|
|
|
|
description = "A path name";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-03 11:08:19 +00:00
|
|
|
|
/* topLevel type of the disko config, takes attrsets of disks, mdadms, zpools, nodevs, and lvm vgs.
|
2022-08-29 09:45:19 +00:00
|
|
|
|
*/
|
2022-10-22 21:35:16 +00:00
|
|
|
|
devices = types.submodule {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
2022-10-22 21:35:16 +00:00
|
|
|
|
disk = mkOption {
|
|
|
|
|
type = types.attrsOf disk;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Block device";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-22 21:35:16 +00:00
|
|
|
|
mdadm = mkOption {
|
|
|
|
|
type = types.attrsOf mdadm;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "mdadm device";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-22 21:35:16 +00:00
|
|
|
|
zpool = mkOption {
|
|
|
|
|
type = types.attrsOf zpool;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "ZFS pool device";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-22 21:35:16 +00:00
|
|
|
|
lvm_vg = mkOption {
|
|
|
|
|
type = types.attrsOf lvm_vg;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "LVM VG device";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-12-03 11:08:19 +00:00
|
|
|
|
nodev = mkOption {
|
|
|
|
|
type = types.attrsOf nodev;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "A non-block device";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-22 21:35:16 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
nodev = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-12-03 11:08:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "nodev" ];
|
|
|
|
|
default = "nodev";
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Device type";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
|
|
|
|
fsType = mkOption {
|
|
|
|
|
type = types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "File system type";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
|
|
|
|
device = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "none";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Device to use";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountpoint = mkOption {
|
|
|
|
|
type = optionTypes.absolute-pathname;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Location to mount the file system at";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountOptions = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
2022-12-16 21:32:44 +00:00
|
|
|
|
default = [ "defaults" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options to pass to mount";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = diskoLib.jsonType;
|
|
|
|
|
default = {
|
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {}: "";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {}: {
|
2022-12-03 11:08:19 +00:00
|
|
|
|
fs.${config.mountpoint} = ''
|
|
|
|
|
if ! findmnt ${config.fsType} "/mnt${config.mountpoint}" > /dev/null 2>&1; then
|
|
|
|
|
mount -t ${config.fsType} ${config.device} "/mnt${config.mountpoint}" \
|
|
|
|
|
${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
|
|
|
|
-o X-mount.mkdir
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = [{
|
|
|
|
|
fileSystems.${config.mountpoint} = {
|
|
|
|
|
device = config.device;
|
|
|
|
|
fsType = config.fsType;
|
|
|
|
|
options = config.mountOptions;
|
|
|
|
|
};
|
|
|
|
|
}];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
2022-12-17 17:09:10 +00:00
|
|
|
|
_pkgs = mkOption {
|
2022-12-03 11:08:19 +00:00
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-12-03 11:08:19 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-12-03 11:08:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
btrfs = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "btrfs" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-12-17 17:09:10 +00:00
|
|
|
|
extraArgs = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Arguments to pass to BTRFS";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
mountOptions = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
2022-12-16 21:32:44 +00:00
|
|
|
|
default = [ "defaults" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "A list of options to pass to mount.";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
subvolumes = mkOption {
|
2022-12-17 17:09:10 +00:00
|
|
|
|
type = types.attrsOf btrfs_subvol;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Subvolumes to define for BTRFS.";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountpoint = mkOption {
|
2022-12-17 17:09:10 +00:00
|
|
|
|
type = types.nullOr optionTypes.absolute-pathname;
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "A path to mount the BTRFS filesystem to.";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-12-17 17:09:10 +00:00
|
|
|
|
default = dev:
|
|
|
|
|
diskoLib.deepMergeMap (subvol: subvol._meta dev) (attrValues config.subvolumes);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-12-17 17:09:10 +00:00
|
|
|
|
mkfs.btrfs ${dev} ${config.extraArgs}
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${concatMapStrings (subvol: subvol._create { inherit dev; }) (attrValues config.subvolumes)}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
2022-12-17 17:09:10 +00:00
|
|
|
|
let
|
2023-01-09 19:35:54 +00:00
|
|
|
|
subvolMounts = diskoLib.deepMergeMap (subvol: subvol._mount {inherit dev; parent = config.mountpoint;}) (attrValues config.subvolumes);
|
2022-12-17 17:09:10 +00:00
|
|
|
|
in {
|
|
|
|
|
fs = subvolMounts.fs // optionalAttrs (!isNull config.mountpoint) {
|
|
|
|
|
${config.mountpoint} = ''
|
|
|
|
|
if ! findmnt ${dev} "/mnt${config.mountpoint}" > /dev/null 2>&1; then
|
|
|
|
|
mount ${dev} "/mnt${config.mountpoint}" \
|
|
|
|
|
${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
|
|
|
|
-o X-mount.mkdir
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = dev: [
|
|
|
|
|
(map (subvol: subvol._config dev config.mountpoint) (attrValues config.subvolumes))
|
|
|
|
|
(optional (!isNull config.mountpoint) {
|
|
|
|
|
fileSystems.${config.mountpoint} = {
|
|
|
|
|
device = dev;
|
|
|
|
|
fsType = "btrfs";
|
|
|
|
|
options = config.mountOptions;
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs:
|
|
|
|
|
[ pkgs.btrfs-progs ] ++ flatten (map (subvolume: subvolume._pkgs pkgs) (attrValues config.subvolumes));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-12-17 17:09:10 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
btrfs_subvol = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-12-17 17:09:10 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the BTRFS subvolume.";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "btrfs_subvol" ];
|
|
|
|
|
default = "btrfs_subvol";
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Extra arguments to pass";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
mountOptions = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [ "defaults" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options to pass to mount";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
mountpoint = mkOption {
|
|
|
|
|
type = types.nullOr optionTypes.absolute-pathname;
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Location to mount the subvolume to.";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev: {
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-12-17 17:09:10 +00:00
|
|
|
|
MNTPOINT=$(mktemp -d)
|
|
|
|
|
(
|
|
|
|
|
mount ${dev} "$MNTPOINT" -o subvol=/
|
|
|
|
|
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
|
|
|
|
btrfs subvolume create "$MNTPOINT"/${config.name} ${config.extraArgs}
|
|
|
|
|
)
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev, parent}: let
|
2022-12-17 17:09:10 +00:00
|
|
|
|
mountpoint = if (!isNull config.mountpoint) then config.mountpoint
|
|
|
|
|
else if (isNull parent) then config.name
|
|
|
|
|
else null;
|
|
|
|
|
in optionalAttrs (!isNull mountpoint) {
|
|
|
|
|
fs.${mountpoint} = ''
|
|
|
|
|
if ! findmnt ${dev} "/mnt${mountpoint}" > /dev/null 2>&1; then
|
|
|
|
|
mount ${dev} "/mnt${mountpoint}" \
|
|
|
|
|
${concatMapStringsSep " " (opt: "-o ${opt}") (config.mountOptions ++ [ "subvol=${config.name}" ])} \
|
2022-08-29 09:45:19 +00:00
|
|
|
|
-o X-mount.mkdir
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-12-17 17:09:10 +00:00
|
|
|
|
default = dev: parent: let
|
|
|
|
|
mountpoint = if (!isNull config.mountpoint) then config.mountpoint
|
|
|
|
|
else if (isNull parent) then config.name
|
|
|
|
|
else null;
|
|
|
|
|
in optional (!isNull mountpoint) {
|
|
|
|
|
fileSystems.${mountpoint} = {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
device = dev;
|
|
|
|
|
fsType = "btrfs";
|
2022-12-17 17:09:10 +00:00
|
|
|
|
options = config.mountOptions ++ [ "subvol=${config.name}" ];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-12-17 17:09:10 +00:00
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-12-17 17:09:10 +00:00
|
|
|
|
_pkgs = mkOption {
|
2022-10-21 20:25:20 +00:00
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
2022-12-17 17:09:10 +00:00
|
|
|
|
default = pkgs: [ pkgs.coreutils ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
filesystem = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "filesystem" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Arguments to pass";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountOptions = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
2022-12-16 21:32:44 +00:00
|
|
|
|
default = [ "defaults" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options to pass to mount";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountpoint = mkOption {
|
|
|
|
|
type = optionTypes.absolute-pathname;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Path to mount the filesystem to";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
format = mkOption {
|
|
|
|
|
type = types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Format of the filesystem";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev: {
|
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
mkfs.${config.format} \
|
|
|
|
|
${config.extraArgs} \
|
|
|
|
|
${dev}
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
fs.${config.mountpoint} = ''
|
|
|
|
|
if ! findmnt ${dev} "/mnt${config.mountpoint}" > /dev/null 2>&1; then
|
|
|
|
|
mount ${dev} "/mnt${config.mountpoint}" \
|
2022-12-29 17:54:20 +00:00
|
|
|
|
-t "${config.format}" \
|
|
|
|
|
${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
|
|
|
|
-o X-mount.mkdir
|
2022-08-29 09:45:19 +00:00
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-10-01 19:29:36 +00:00
|
|
|
|
default = dev: [{
|
2022-08-29 09:45:19 +00:00
|
|
|
|
fileSystems.${config.mountpoint} = {
|
|
|
|
|
device = dev;
|
|
|
|
|
fsType = config.format;
|
2022-12-16 21:32:44 +00:00
|
|
|
|
options = config.mountOptions;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-01 19:29:36 +00:00
|
|
|
|
}];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
# type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs:
|
2022-10-29 11:19:39 +00:00
|
|
|
|
[ pkgs.util-linux ] ++ (
|
|
|
|
|
# TODO add many more
|
|
|
|
|
if (config.format == "xfs") then [ pkgs.xfsprogs ]
|
|
|
|
|
else if (config.format == "btrfs") then [ pkgs.btrfs-progs ]
|
|
|
|
|
else if (config.format == "vfat") then [ pkgs.dosfstools ]
|
|
|
|
|
else if (config.format == "ext2") then [ pkgs.e2fsprogs ]
|
|
|
|
|
else if (config.format == "ext3") then [ pkgs.e2fsprogs ]
|
|
|
|
|
else if (config.format == "ext4") then [ pkgs.e2fsprogs ]
|
2022-12-29 17:46:31 +00:00
|
|
|
|
else if (config.format == "bcachefs") then [ pkgs.bcachefs-tools ]
|
2022-10-29 11:19:39 +00:00
|
|
|
|
else []
|
|
|
|
|
);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
table = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "table" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Partition table";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
format = mkOption {
|
|
|
|
|
type = types.enum [ "gpt" "msdos" ];
|
|
|
|
|
default = "gpt";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "The kind of partition table";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
partitions = mkOption {
|
|
|
|
|
type = types.listOf partition;
|
|
|
|
|
default = [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "List of partitions to add to the partition table";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev:
|
|
|
|
|
diskoLib.deepMergeMap (partition: partition._meta dev) config.partitions;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-11-25 07:38:05 +00:00
|
|
|
|
parted -s ${dev} -- mklabel ${config.format}
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${concatMapStrings (partition: partition._create {inherit dev; type = config.format;} ) config.partitions}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
2022-08-29 09:45:19 +00:00
|
|
|
|
let
|
2023-01-09 19:35:54 +00:00
|
|
|
|
partMounts = diskoLib.deepMergeMap (partition: partition._mount {inherit dev;}) config.partitions;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
in {
|
|
|
|
|
dev = ''
|
2023-01-06 15:08:29 +00:00
|
|
|
|
${concatMapStrings (x: x.dev or "") (attrValues partMounts)}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
fs = partMounts.fs or {};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = dev:
|
2022-10-01 19:29:36 +00:00
|
|
|
|
map (partition: partition._config dev) config.partitions;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs:
|
2022-10-29 11:19:39 +00:00
|
|
|
|
[ pkgs.parted pkgs.systemdMinimal ] ++ flatten (map (partition: partition._pkgs pkgs) config.partitions);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
partition = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "partition" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
part-type = mkOption {
|
|
|
|
|
type = types.enum [ "primary" "logical" "extended" ];
|
|
|
|
|
default = "primary";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Partition type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
fs-type = mkOption {
|
|
|
|
|
type = types.nullOr (types.enum [ "btrfs" "ext2" "ext3" "ext4" "fat16" "fat32" "hfs" "hfs+" "linux-swap" "ntfs" "reiserfs" "udf" "xfs" ]);
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Filesystem type to use";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.nullOr types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the partition";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
start = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "0%";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Start of the partition";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
end = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "100%";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "End of the partition";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
index = mkOption {
|
|
|
|
|
type = types.int;
|
|
|
|
|
# TODO find a better way to get the index
|
|
|
|
|
default = toInt (head (match ".*entry ([[:digit:]]+)]" config._module.args.name));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Index of the partition";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
flags = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Partition flags";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
bootable = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Whether to make the partition bootable";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
content = diskoLib.partitionType;
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._meta dev);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev, type}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
${optionalString (type == "gpt") ''
|
2022-11-25 07:38:05 +00:00
|
|
|
|
parted -s ${dev} -- mkpart ${config.name} ${diskoLib.maybeStr config.fs-type} ${config.start} ${config.end}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
''}
|
|
|
|
|
${optionalString (type == "msdos") ''
|
2022-11-25 07:38:05 +00:00
|
|
|
|
parted -s ${dev} -- mkpart ${config.part-type} ${diskoLib.maybeStr config.fs-type} ${diskoLib.maybeStr config.fs-type} ${config.start} ${config.end}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
''}
|
|
|
|
|
# ensure /dev/disk/by-path/..-partN exists before continuing
|
|
|
|
|
udevadm trigger --subsystem-match=block; udevadm settle
|
|
|
|
|
${optionalString (config.bootable) ''
|
2022-11-25 07:38:05 +00:00
|
|
|
|
parted -s ${dev} -- set ${toString config.index} boot on
|
2022-08-29 09:45:19 +00:00
|
|
|
|
''}
|
|
|
|
|
${concatMapStringsSep "" (flag: ''
|
2022-11-25 07:38:05 +00:00
|
|
|
|
parted -s ${dev} -- set ${toString config.index} ${flag} on
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'') config.flags}
|
2022-09-12 10:32:35 +00:00
|
|
|
|
# ensure further operations can detect new partitions
|
|
|
|
|
udevadm trigger --subsystem-match=block; udevadm settle
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${optionalString (!isNull config.content) (config.content._create {dev = (diskoLib.deviceNumbering dev config.index);})}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._mount {dev = (diskoLib.deviceNumbering dev config.index);});
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = dev:
|
2022-10-01 19:29:36 +00:00
|
|
|
|
optional (!isNull config.content) (config.content._config (diskoLib.deviceNumbering dev config.index));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: optionals (!isNull config.content) (config.content._pkgs pkgs);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
swap = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-11-05 20:17:35 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "swap" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-11-05 20:17:35 +00:00
|
|
|
|
};
|
|
|
|
|
randomEncryption = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Whether to randomly encrypt the swap";
|
2022-11-05 20:17:35 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
|
|
|
|
default = dev: {
|
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-11-05 20:17:35 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-11-05 20:17:35 +00:00
|
|
|
|
mkswap ${dev}
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}: {
|
2022-11-05 20:17:35 +00:00
|
|
|
|
fs.${dev} = ''
|
2022-12-21 12:51:27 +00:00
|
|
|
|
if ! swapon --show | grep -q '^${dev} '; then
|
2022-11-05 20:17:35 +00:00
|
|
|
|
swapon ${dev}
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = dev: [{
|
|
|
|
|
swapDevices = [{
|
|
|
|
|
device = dev;
|
|
|
|
|
randomEncryption = config.randomEncryption;
|
|
|
|
|
}];
|
|
|
|
|
}];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-11-05 20:17:35 +00:00
|
|
|
|
};
|
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
2022-12-11 23:12:21 +00:00
|
|
|
|
default = pkgs: [ pkgs.gnugrep pkgs.util-linux ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-11-05 20:17:35 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-11-05 20:17:35 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
lvm_pv = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "lvm_pv" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
vg = mkOption {
|
|
|
|
|
type = types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Volume group";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev: {
|
2022-10-21 12:44:17 +00:00
|
|
|
|
deviceDependencies.lvm_vg.${config.vg} = [ dev ];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
pvcreate ${dev}
|
|
|
|
|
LVMDEVICES_${config.vg}="''${LVMDEVICES_${config.vg}:-}${dev} "
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
2022-08-29 09:45:19 +00:00
|
|
|
|
{};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-10-01 19:29:36 +00:00
|
|
|
|
default = dev: [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: [ pkgs.lvm2 ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
lvm_vg = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the volume gorup";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "lvm_vg" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
lvs = mkOption {
|
|
|
|
|
type = types.attrsOf lvm_lv;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "LVS for the volume group";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default =
|
|
|
|
|
diskoLib.deepMergeMap (lv: lv._meta [ "lvm_vg" config.name ]) (attrValues config.lvs);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
vgcreate ${config.name} $LVMDEVICES_${config.name}
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${concatMapStrings (lv: lv._create {vg = config.name; }) (attrValues config.lvs)}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {}: let
|
|
|
|
|
lvMounts = diskoLib.deepMergeMap (lv: lv._mount {vg = config.name;}) (attrValues config.lvs);
|
2022-08-29 09:45:19 +00:00
|
|
|
|
in {
|
|
|
|
|
dev = ''
|
|
|
|
|
vgchange -a y
|
2023-01-06 15:08:29 +00:00
|
|
|
|
${concatMapStrings (x: x.dev or "") (attrValues lvMounts)}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
fs = lvMounts.fs;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default =
|
2022-10-01 19:29:36 +00:00
|
|
|
|
map (lv: lv._config config.name) (attrValues config.lvs);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: flatten (map (lv: lv._pkgs pkgs) (attrValues config.lvs));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
lvm_lv = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the logical volume";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "lvm_lv" ];
|
|
|
|
|
default = "lvm_lv";
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
size = mkOption {
|
|
|
|
|
type = types.str; # TODO lvm size type
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Size of the logical volume";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
lvm_type = mkOption {
|
|
|
|
|
type = types.nullOr (types.enum [ "mirror" "raid0" "raid1" ]); # TODO add all types
|
|
|
|
|
default = null; # maybe there is always a default type?
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "LVM type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Extra arguments";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
content = diskoLib.partitionType;
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._meta dev);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {vg}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
lvcreate \
|
2022-12-01 14:39:11 +00:00
|
|
|
|
--yes \
|
2022-08-29 09:45:19 +00:00
|
|
|
|
${if hasInfix "%" config.size then "-l" else "-L"} ${config.size} \
|
|
|
|
|
-n ${config.name} \
|
|
|
|
|
${optionalString (!isNull config.lvm_type) "--type=${config.lvm_type}"} \
|
|
|
|
|
${config.extraArgs} \
|
|
|
|
|
${vg}
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${optionalString (!isNull config.content) (config.content._create {dev = "/dev/${vg}/${config.name}";})}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {vg}:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._mount {dev = "/dev/${vg}/${config.name}";});
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-30 17:50:32 +00:00
|
|
|
|
default = vg:
|
2022-10-01 19:29:36 +00:00
|
|
|
|
[
|
|
|
|
|
(optional (!isNull config.content) (config.content._config "/dev/${vg}/${config.name}"))
|
|
|
|
|
(optional (!isNull config.lvm_type) {
|
|
|
|
|
boot.initrd.kernelModules = [ "dm-${config.lvm_type}" ];
|
|
|
|
|
})
|
|
|
|
|
];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: lib.optionals (!isNull config.content) (config.content._pkgs pkgs);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
zfs = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "zfs" ];
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
pool = mkOption {
|
|
|
|
|
type = types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the ZFS pool";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev: {
|
2022-10-21 12:44:17 +00:00
|
|
|
|
deviceDependencies.zpool.${config.pool} = [ dev ];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
ZFSDEVICES_${config.pool}="''${ZFSDEVICES_${config.pool}:-}${dev} "
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
2022-08-29 09:45:19 +00:00
|
|
|
|
{};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-10-01 19:29:36 +00:00
|
|
|
|
default = dev: [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: [ pkgs.zfs ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
zpool = types.submodule [diskoLib.hookMixin ({ config, options, ... }:
|
2023-01-06 14:58:32 +00:00
|
|
|
|
{
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the ZFS pool";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "zpool" ];
|
2022-12-03 13:12:57 +00:00
|
|
|
|
default = "zpool";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mode = mkOption {
|
|
|
|
|
type = types.str; # TODO zfs modes
|
|
|
|
|
default = "";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Mode of the ZFS pool";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
options = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options for the ZFS pool";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
rootFsOptions = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options for the root filesystem";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountpoint = mkOption {
|
|
|
|
|
type = types.nullOr optionTypes.absolute-pathname;
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "The mountpoint of the pool";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountOptions = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
2022-12-16 21:32:44 +00:00
|
|
|
|
default = [ "defaults" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options to pass to mount";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
datasets = mkOption {
|
|
|
|
|
type = types.attrsOf zfs_dataset;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "List of datasets to define";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default =
|
|
|
|
|
diskoLib.deepMergeMap (dataset: dataset._meta [ "zpool" config.name ]) (attrValues config.datasets);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
zpool create ${config.name} \
|
|
|
|
|
${config.mode} \
|
|
|
|
|
${concatStringsSep " " (mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
|
|
|
|
|
${concatStringsSep " " (mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
|
|
|
|
|
''${ZFSDEVICES_${config.name}}
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${concatMapStrings (dataset: dataset._create {zpool = config.name;}) (attrValues config.datasets)}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {}: let
|
|
|
|
|
datasetMounts = diskoLib.deepMergeMap (dataset: dataset._mount {zpool = config.name;}) (attrValues config.datasets);
|
2022-08-29 09:45:19 +00:00
|
|
|
|
in {
|
|
|
|
|
dev = ''
|
|
|
|
|
zpool list '${config.name}' >/dev/null 2>/dev/null || zpool import '${config.name}'
|
2023-01-06 15:08:29 +00:00
|
|
|
|
${concatMapStrings (x: x.dev or "") (attrValues datasetMounts)}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
fs = datasetMounts.fs // optionalAttrs (!isNull config.mountpoint) {
|
|
|
|
|
${config.mountpoint} = ''
|
|
|
|
|
if ! findmnt ${config.name} "/mnt${config.mountpoint}" > /dev/null 2>&1; then
|
|
|
|
|
mount ${config.name} "/mnt${config.mountpoint}" \
|
|
|
|
|
${optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
|
2022-12-03 13:12:33 +00:00
|
|
|
|
${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
2022-08-29 09:45:19 +00:00
|
|
|
|
-o X-mount.mkdir \
|
|
|
|
|
-t zfs
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-10-21 20:25:20 +00:00
|
|
|
|
default = [
|
|
|
|
|
(map (dataset: dataset._config config.name) (attrValues config.datasets))
|
|
|
|
|
(optional (!isNull config.mountpoint) {
|
|
|
|
|
fileSystems.${config.mountpoint} = {
|
|
|
|
|
device = config.name;
|
|
|
|
|
fsType = "zfs";
|
2022-12-16 21:32:44 +00:00
|
|
|
|
options = config.mountOptions ++ lib.optional ((config.options.mountpoint or "") != "legacy") "zfsutil";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
2022-10-29 11:19:39 +00:00
|
|
|
|
default = pkgs: [ pkgs.util-linux ] ++ flatten (map (dataset: dataset._pkgs pkgs) (attrValues config.datasets));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
zfs_dataset = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the dataset";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "zfs_dataset" ];
|
|
|
|
|
default = "zfs_dataset";
|
2022-09-04 09:46:37 +00:00
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
zfs_type = mkOption {
|
|
|
|
|
type = types.enum [ "filesystem" "volume" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "The type of the dataset";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
options = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Options to set for the dataset";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
mountOptions = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
2022-12-16 21:32:44 +00:00
|
|
|
|
default = [ "defaults" ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Mount options";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# filesystem options
|
|
|
|
|
mountpoint = mkOption {
|
|
|
|
|
type = types.nullOr optionTypes.absolute-pathname;
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Path to mount the dataset to";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# volume options
|
|
|
|
|
size = mkOption {
|
|
|
|
|
type = types.nullOr types.str; # TODO size
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Size of the dataset";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
content = diskoLib.partitionType;
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._meta dev);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {zpool}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
zfs create ${zpool}/${config.name} \
|
|
|
|
|
${concatStringsSep " " (mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
|
|
|
|
|
${optionalString (config.zfs_type == "volume") "-V ${config.size}"}
|
|
|
|
|
${optionalString (config.zfs_type == "volume") ''
|
|
|
|
|
udevadm trigger --subsystem-match=block; udevadm settle
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${optionalString (!isNull config.content) (config.content._create {dev = "/dev/zvol/${zpool}/${config.name}";})}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
''}
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {zpool}:
|
|
|
|
|
optionalAttrs (config.zfs_type == "volume" && !isNull config.content) (config.content._mount {dev = "/dev/zvol/${zpool}/${config.name}";}) //
|
2022-08-29 09:45:19 +00:00
|
|
|
|
optionalAttrs (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") { fs.${config.mountpoint} = ''
|
|
|
|
|
if ! findmnt ${zpool}/${config.name} "/mnt${config.mountpoint}" > /dev/null 2>&1; then
|
|
|
|
|
mount ${zpool}/${config.name} "/mnt${config.mountpoint}" \
|
|
|
|
|
-o X-mount.mkdir \
|
2022-12-03 13:12:33 +00:00
|
|
|
|
${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
2022-08-29 09:45:19 +00:00
|
|
|
|
${optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
|
|
|
|
|
-t zfs
|
|
|
|
|
fi
|
|
|
|
|
''; };
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = zpool:
|
2022-10-01 19:29:36 +00:00
|
|
|
|
(optional (config.zfs_type == "volume" && !isNull config.content) (config.content._config "/dev/zvol/${zpool}/${config.name}")) ++
|
|
|
|
|
(optional (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") {
|
|
|
|
|
fileSystems.${config.mountpoint} = {
|
|
|
|
|
device = "${zpool}/${config.name}";
|
|
|
|
|
fsType = "zfs";
|
2022-12-16 21:32:44 +00:00
|
|
|
|
options = config.mountOptions ++ lib.optional ((config.options.mountpoint or "") != "legacy") "zfsutil";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-01 19:29:36 +00:00
|
|
|
|
});
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
2022-10-29 11:19:39 +00:00
|
|
|
|
default = pkgs: [ pkgs.util-linux ] ++ lib.optionals (!isNull config.content) (config.content._pkgs pkgs);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
mdadm = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "mdadm" ];
|
|
|
|
|
default = "mdadm";
|
2022-09-04 09:46:37 +00:00
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
level = mkOption {
|
|
|
|
|
type = types.int;
|
|
|
|
|
default = 1;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "mdadm level";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-01 17:03:42 +00:00
|
|
|
|
metadata = mkOption {
|
|
|
|
|
type = types.enum [ "1" "1.0" "1.1" "1.2" "default" "ddf" "imsm" ];
|
|
|
|
|
default = "default";
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-10-01 17:03:42 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
content = diskoLib.deviceType;
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default =
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._meta [ "mdadm" config.name ]);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
echo 'y' | mdadm --create /dev/md/${config.name} \
|
|
|
|
|
--level=${toString config.level} \
|
|
|
|
|
--raid-devices=''${RAIDDEVICES_N_${config.name}} \
|
2022-10-01 17:03:42 +00:00
|
|
|
|
--metadata=${config.metadata} \
|
2022-12-23 11:37:57 +00:00
|
|
|
|
--force \
|
2022-10-01 17:03:42 +00:00
|
|
|
|
--homehost=any \
|
2022-08-29 09:45:19 +00:00
|
|
|
|
''${RAIDDEVICES_${config.name}}
|
|
|
|
|
udevadm trigger --subsystem-match=block; udevadm settle
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${optionalString (!isNull config.content) (config.content._create {dev = "/dev/md/${config.name}";})}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {}:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._mount {dev = "/dev/md/${config.name}";});
|
2022-08-29 09:45:19 +00:00
|
|
|
|
# TODO we probably need to assemble the mdadm somehow
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default =
|
2022-10-01 19:29:36 +00:00
|
|
|
|
optional (!isNull config.content) (config.content._config "/dev/md/${config.name}");
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: (lib.optionals (!isNull config.content) (config.content._pkgs pkgs));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
mdraid = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "mdraid" ];
|
2022-09-04 09:46:37 +00:00
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev: {
|
2022-10-21 12:44:17 +00:00
|
|
|
|
deviceDependencies.mdadm.${config.name} = [ dev ];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
RAIDDEVICES_N_${config.name}=$((''${RAIDDEVICES_N_${config.name}:-0}+1))
|
|
|
|
|
RAIDDEVICES_${config.name}="''${RAIDDEVICES_${config.name}:-}${dev} "
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
2022-08-29 09:45:19 +00:00
|
|
|
|
{};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-10-01 19:29:36 +00:00
|
|
|
|
default = dev: [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: [ pkgs.mdadm ];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
luks = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "luks" ];
|
2022-09-04 09:46:37 +00:00
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Name of the LUKS";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
keyFile = mkOption {
|
|
|
|
|
type = types.nullOr optionTypes.absolute-pathname;
|
|
|
|
|
default = null;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Path to the key for encryption";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Extra arguments";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
content = diskoLib.deviceType;
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = types.functionTo diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default = dev:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._meta dev);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {dev}: ''
|
2022-08-29 09:45:19 +00:00
|
|
|
|
cryptsetup -q luksFormat ${dev} ${diskoLib.maybeStr config.keyFile} ${toString config.extraArgs}
|
|
|
|
|
cryptsetup luksOpen ${dev} ${config.name} ${optionalString (!isNull config.keyFile) "--key-file ${config.keyFile}"}
|
2023-01-09 19:18:17 +00:00
|
|
|
|
${optionalString (!isNull config.content) (config.content._create {dev = "/dev/mapper/${config.name}";})}
|
2022-08-29 09:45:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {dev}:
|
2022-08-29 09:45:19 +00:00
|
|
|
|
let
|
2023-01-09 19:35:54 +00:00
|
|
|
|
contentMount = config.content._mount {dev = "/dev/mapper/${config.name}";};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
dev = ''
|
|
|
|
|
cryptsetup status ${config.name} >/dev/null 2>/dev/null ||
|
|
|
|
|
cryptsetup luksOpen ${dev} ${config.name} ${optionalString (!isNull config.keyFile) "--key-file ${config.keyFile}"}
|
|
|
|
|
${optionalString (!isNull config.content) contentMount.dev or ""}
|
|
|
|
|
'';
|
|
|
|
|
fs = optionalAttrs (!isNull config.content) contentMount.fs or {};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default = dev:
|
2022-10-01 19:29:36 +00:00
|
|
|
|
[
|
2022-08-29 09:45:19 +00:00
|
|
|
|
# TODO do we need this always in initrd and only there?
|
2022-10-01 19:29:36 +00:00
|
|
|
|
{ boot.initrd.luks.devices.${config.name}.device = dev; }
|
|
|
|
|
] ++ (optional (!isNull config.content) (config.content._config "/dev/mapper/${config.name}"));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (!isNull config.content) (config.content._pkgs pkgs));
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
|
2023-01-06 18:59:47 +00:00
|
|
|
|
disk = types.submodule [diskoLib.hookMixin ({ config, options, ... }: {
|
2022-08-29 09:45:19 +00:00
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = config._module.args.name;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Device name";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
type = mkOption {
|
|
|
|
|
type = types.enum [ "disk" ];
|
2022-12-03 13:12:57 +00:00
|
|
|
|
default = "disk";
|
|
|
|
|
internal = true;
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Type";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
device = mkOption {
|
2022-11-05 20:17:35 +00:00
|
|
|
|
type = optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Device path";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
content = diskoLib.deviceType;
|
|
|
|
|
_meta = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
2022-09-13 11:21:37 +00:00
|
|
|
|
type = diskoLib.jsonType;
|
2022-08-29 09:45:19 +00:00
|
|
|
|
default =
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._meta [ "disk" config.device ]);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Metadata";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 09:25:21 +00:00
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:18:17 +00:00
|
|
|
|
default = {}: config.content._create {dev = config.device;};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-10 15:36:13 +00:00
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
2023-01-09 19:35:54 +00:00
|
|
|
|
default = {}:
|
|
|
|
|
optionalAttrs (!isNull config.content) (config.content._mount {dev = config.device;});
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
|
|
|
|
_config = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
default =
|
2022-10-01 19:29:36 +00:00
|
|
|
|
optional (!isNull config.content) (config.content._config config.device);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "NixOS configuration";
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2022-10-21 20:25:20 +00:00
|
|
|
|
_pkgs = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
2022-12-01 14:39:11 +00:00
|
|
|
|
default = pkgs: [ pkgs.jq ] ++ lib.optionals (!isNull config.content) (config.content._pkgs pkgs);
|
2023-01-03 18:52:57 +00:00
|
|
|
|
description = "Packages";
|
2022-10-21 20:25:20 +00:00
|
|
|
|
};
|
2022-08-29 09:45:19 +00:00
|
|
|
|
};
|
2023-01-06 18:59:47 +00:00
|
|
|
|
})];
|
2022-08-29 09:45:19 +00:00
|
|
|
|
}
|