lib.body.*: don't require initial (unused) state

This commit is contained in:
lassulus 2018-07-31 22:08:44 +02:00
parent 9cbf541974
commit 348d4f164f
2 changed files with 36 additions and 29 deletions

View File

@ -3,7 +3,7 @@
with import ../lib; with import ../lib;
{ {
config = config "/dev/sda" (import ./config.nix); config = config (import ./config.nix);
create = create "/dev/sda" (import ./config.nix); create = create (import ./config.nix);
mount = mount "/dev/sda" (import ./config.nix); mount = mount (import ./config.nix);
} }

View File

@ -3,11 +3,13 @@ with builtins;
let { let {
body.config = q: x: config.${x.type} q x; body.config = config-f {};
body.create = q: x: create.${x.type} q x; body.create = create-f {};
body.mount = q: x: mount.${x.type} q x; body.mount = mount-f {};
config-f = q: x: config.${x.type} q x;
config.filesystem = q: x: { config.filesystem = q: x: {
fileSystems.${x.mountpoint} = { fileSystems.${x.mountpoint} = {
device = q.device; device = q.device;
@ -16,48 +18,50 @@ let {
}; };
config.devices = q: x: config.devices = q: x:
foldl' mergeAttrs {} (mapAttrsToList (name: body.config { device = "/dev/${name}"; }) x.content); foldl' mergeAttrs {} (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;
} // body.config { device = "/dev/mapper/${x.name}"; } x.content; } // config-f { device = "/dev/mapper/${x.name}"; } x.content;
config.lv = q: x: config.lv = q: x:
body.config { device = "/dev/${q.vgname}/${q.name}"; } x.content; config-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
config.lvm = q: x: config.lvm = q: x:
foldl' mergeAttrs {} (mapAttrsToList (name: body.config { inherit name; vgname = x.name; }) x.lvs); foldl' mergeAttrs {} (mapAttrsToList (name: config-f { inherit name; vgname = x.name; }) x.lvs);
config.partition = q: x: config.partition = q: x:
body.config { 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' mergeAttrs {} (imap (index: body.config (q // { inherit index; })) x.partitions); foldl' mergeAttrs {} (imap (index: config-f (q // { inherit index; })) x.partitions);
create-f = q: x: create.${x.type} q x;
create.filesystem = q: x: '' create.filesystem = q: x: ''
mkfs.${x.format} ${q.device} mkfs.${x.format} ${q.device}
''; '';
create.devices = q: x: '' create.devices = q: x: ''
${concatStrings (mapAttrsToList (name: body.create { device = "/dev/${name}"; }) x.content)} ${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; }) x.content)}
''; '';
create.luks = q: x: '' create.luks = q: x: ''
cryptsetup -q luksFormat ${q.device} ${x.keyfile} ${toString (x.extraArgs or [])} cryptsetup -q luksFormat ${q.device} ${x.keyfile} ${toString (x.extraArgs or [])}
cryptsetup luksOpen ${q.device} ${x.name} --key-file ${x.keyfile} cryptsetup luksOpen ${q.device} ${x.name} --key-file ${x.keyfile}
${body.create { device = "/dev/mapper/${x.name}"; } x.content} ${create-f { device = "/dev/mapper/${x.name}"; } x.content}
''; '';
create.lv = q: x: '' create.lv = q: x: ''
lvcreate -L ${x.size} -n ${q.name} ${q.vgname} lvcreate -L ${x.size} -n ${q.name} ${q.vgname}
${body.create { device = "/dev/${q.vgname}/${q.name}"; } x.content} ${create-f { device = "/dev/${q.vgname}/${q.name}"; } x.content}
''; '';
create.lvm = q: x: '' create.lvm = q: x: ''
pvcreate ${q.device} pvcreate ${q.device}
vgcreate ${x.name} ${q.device} vgcreate ${x.name} ${q.device}
${concatStrings (mapAttrsToList (name: body.create { inherit name; vgname = x.name; }) x.lvs)} ${concatStrings (mapAttrsToList (name: create-f { inherit name; vgname = x.name; }) x.lvs)}
''; '';
create.partition = q: x: '' create.partition = q: x: ''
@ -65,23 +69,26 @@ let {
${optionalString (x.bootable or false) '' ${optionalString (x.bootable or false) ''
parted -s ${q.device} set ${toString q.index} boot on parted -s ${q.device} set ${toString q.index} boot on
''} ''}
${body.create { device = q.device + toString q.index; } x.content} ${create-f { device = q.device + toString q.index; } x.content}
''; '';
create.table = q: x: '' create.table = q: x: ''
parted -s ${q.device} mklabel ${x.format} parted -s ${q.device} mklabel ${x.format}
${concatStrings (imap (index: body.create (q // { inherit index; })) x.partitions)} ${concatStrings (imap (index: create-f (q // { inherit index; })) x.partitions)}
''; '';
mount-f = q: x: mount.${x.type} q x;
mount.filesystem = q: x: { mount.filesystem = q: x: {
fs.${x.mountpoint} = '' fs.${x.mountpoint} = ''
mkdir -p ${x.mountpoint} mkdir -p ${x.mountpoint}
mount ${q.device} ${x.mountpoint} mount ${q.device} ${x.mountpoint}
''; '';
}; };
mount.devices = q: x: let mount.devices = q: x: let
z = foldl' recursiveUpdate {} (mapAttrsToList (name: body.mount { device = "/dev/${name}"; }) x.content); z = foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { device = "/dev/${name}"; }) x.content);
# attrValues returns values sorted by name. This is important, because it # attrValues returns values sorted by name. This is important, because it
# ensures that "/" is processed before "/foo" etc. # ensures that "/" is processed before "/foo" etc.
in '' in ''
@ -92,27 +99,27 @@ let {
mount.luks = q: x: ( mount.luks = q: x: (
recursiveUpdate recursiveUpdate
(body.mount { device = "/dev/mapper/${x.name}"; } x.content) (mount-f { device = "/dev/mapper/${x.name}"; } x.content)
{luks.${q.device} = '' {luks.${q.device} = ''
cryptsetup luksOpen ${q.device} ${x.name} --key-file ${x.keyfile} cryptsetup luksOpen ${q.device} ${x.name} --key-file ${x.keyfile}
'';} '';}
); );
mount.lv = q: x: mount.lv = q: x:
body.mount { device = "/dev/${q.vgname}/${q.name}"; } x.content; mount-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
mount.lvm = q: x: ( mount.lvm = q: x: (
recursiveUpdate recursiveUpdate
(foldl' recursiveUpdate {} (mapAttrsToList (name: body.mount { inherit name; vgname = x.name; }) x.lvs)) (foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { inherit name; vgname = x.name; }) x.lvs))
{lvm.${q.device} = '' {lvm.${q.device} = ''
vgchange -a y vgchange -a y
'';} '';}
); );
mount.partition = q: x: mount.partition = q: x:
body.mount { device = q.device + toString q.index; } x.content; mount-f { device = q.device + toString q.index; } x.content;
mount.table = q: x: mount.table = q: x:
foldl' recursiveUpdate {} (imap (index: body.mount (q // { inherit index; })) x.partitions); foldl' recursiveUpdate {} (imap (index: mount-f (q // { inherit index; })) x.partitions);
} }