turn disk configs into valid nixos configuration

This commit is contained in:
lassulus 2023-03-03 08:09:13 +01:00
parent cd825b85fb
commit 769bde0834
24 changed files with 1152 additions and 1186 deletions

View file

@ -7,9 +7,9 @@ let
eval = cfg: lib.evalModules { eval = cfg: lib.evalModules {
modules = lib.singleton { modules = lib.singleton {
# _file = toString input; # _file = toString input;
imports = lib.singleton { devices = cfg; }; imports = lib.singleton { disko.devices = cfg.disko.devices; };
options = { options = {
devices = lib.mkOption { disko.devices = lib.mkOption {
type = types.devices; type = types.devices;
}; };
}; };
@ -18,30 +18,30 @@ let
in in
{ {
types = types; types = types;
create = cfg: types.diskoLib.create (eval cfg).config.devices; create = cfg: types.diskoLib.create (eval cfg).config.disko.devices;
createScript = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-create" '' createScript = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-create" ''
export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.devices pkgs)}:$PATH export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.disko.devices pkgs)}:$PATH
${types.diskoLib.create (eval cfg).config.devices} ${types.diskoLib.create (eval cfg).config.disko.devices}
''; '';
createScriptNoDeps = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-create" '' createScriptNoDeps = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-create" ''
${types.diskoLib.create (eval cfg).config.devices} ${types.diskoLib.create (eval cfg).config.disko.devices}
''; '';
mount = cfg: types.diskoLib.mount (eval cfg).config.devices; mount = cfg: types.diskoLib.mount (eval cfg).config.disko.devices;
mountScript = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-mount" '' mountScript = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-mount" ''
export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.devices pkgs)}:$PATH export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.disko.devices pkgs)}:$PATH
${types.diskoLib.mount (eval cfg).config.devices} ${types.diskoLib.mount (eval cfg).config.disko.devices}
''; '';
mountScriptNoDeps = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" '' mountScriptNoDeps = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" ''
${types.diskoLib.mount (eval cfg).config.devices} ${types.diskoLib.mount (eval cfg).config.disko.devices}
''; '';
zapCreateMount = cfg: types.diskoLib.zapCreateMount (eval cfg).config.devices; zapCreateMount = cfg: types.diskoLib.zapCreateMount (eval cfg).config.disko.devices;
zapCreateMountScript = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-zap-create-mount" '' zapCreateMountScript = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-zap-create-mount" ''
export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.devices pkgs)}:$PATH export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.disko.devices pkgs)}:$PATH
${types.diskoLib.zapCreateMount (eval cfg).config.devices} ${types.diskoLib.zapCreateMount (eval cfg).config.disko.devices}
''; '';
zapCreateMountScriptNoDeps = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-zap-create-mount" '' zapCreateMountScriptNoDeps = cfg: pkgs: (types.diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-zap-create-mount" ''
${types.diskoLib.zapCreateMount (eval cfg).config.devices} ${types.diskoLib.zapCreateMount (eval cfg).config.disko.devices}
''; '';
config = cfg: { imports = types.diskoLib.config (eval cfg).config.devices; }; config = cfg: { imports = types.diskoLib.config (eval cfg).config.disko.devices; };
packages = cfg: types.diskoLib.packages (eval cfg).config.devices; packages = cfg: types.diskoLib.packages (eval cfg).config.disko.devices;
} }

View file

@ -106,10 +106,10 @@ imports =
[ # Include the results of the hardware scan. [ # Include the results of the hardware scan.
./hardware-configuration.nix ./hardware-configuration.nix
"${builtins.fetchTarball "https://github.com/nix-community/disko/archive/master.tar.gz"}/module.nix" "${builtins.fetchTarball "https://github.com/nix-community/disko/archive/master.tar.gz"}/module.nix"
]; (import ./disko-config.nix {
disko.devices = pkgs.callPackage ./disko-config.nix {
disks = [ "/dev/<disk-name>" ]; # replace this with your disk name i.e. /dev/nvme0n1 disks = [ "/dev/<disk-name>" ]; # replace this with your disk name i.e. /dev/nvme0n1
}; })
];
``` ```
If you went for the hybrid-partition scheme, than choose grub as a bootloader. If you went for the hybrid-partition scheme, than choose grub as a bootloader.

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -35,5 +36,6 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
one = { one = {
type = "disk"; type = "disk";
@ -114,4 +115,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
type = "disk"; type = "disk";
@ -48,5 +49,6 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" "/dev/vdd" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" "/dev/vdd" ], ... }: {
disko.devices = {
disk = { disk = {
disk0 = { disk0 = {
type = "disk"; type = "disk";
@ -196,4 +197,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,69 +0,0 @@
# usage: nix-instantiate --eval --json --strict example/config.nix | jq .
{
lvm_vg = {
pool = {
type = "lvm_vg";
lvs = {
root = {
type = "lvm_lv";
size = "10G";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
};
};
home = {
type = "lvm_lv";
size = "10G";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/home";
};
};
};
};
};
disk = {
sda = {
device = "/dev/sda";
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "ESP";
type = "partition";
start = "1MiB";
end = "1024MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
}
{
name = "crypt_root";
type = "partition";
part-type = "primary";
start = "1024MiB";
end = "100%";
flags = [ "bios_grub" ];
content = {
type = "luks";
name = "crypted";
keyFile = "/tmp/secret.key";
content = {
type = "lvm_pv";
vg = "pool";
};
};
}
];
};
};
};
}

View file

@ -1,5 +1,6 @@
# Example to create a bios compatible gpt partition # Example to create a bios compatible gpt partition
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -34,4 +35,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,5 +1,5 @@
{ disks ? [ "/dev/sda" ], ... }: { disks ? [ "/dev/sda" ], ... }: {
{ disko.devices = {
disk.main = { disk.main = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
type = "disk"; type = "disk";
@ -49,4 +49,5 @@
"mode=755" "mode=755"
]; ];
}; };
};
} }

View file

@ -1,5 +1,5 @@
{ disks ? [ "/dev/vda" ], ... }: { disks ? [ "/dev/vda" ], ... }: {
{ disko.devices = {
disk = { disk = {
main = { main = {
type = "disk"; type = "disk";
@ -42,5 +42,6 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
type = "disk"; type = "disk";
@ -74,4 +75,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
one = { one = {
type = "disk"; type = "disk";
@ -107,4 +108,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
type = "disk"; type = "disk";
@ -80,4 +81,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
disk0 = { disk0 = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -47,4 +48,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
disk0 = { disk0 = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -24,4 +25,5 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -36,5 +37,6 @@
}; };
}; };
}; };
};
} }

View file

@ -14,7 +14,7 @@ let
#}) { #}) {
# inherit lib; # inherit lib;
#}; #};
cfg = { cfg.disko.devices = {
disk = { disk = {
sda = { sda = {
device = "/dev/sda"; device = "/dev/sda";

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -46,5 +47,6 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" ], ... }: { { disks ? [ "/dev/vdb" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
device = builtins.elemAt disks 0; device = builtins.elemAt disks 0;
@ -44,5 +45,6 @@
]; ];
}; };
}; };
};
} }

View file

@ -1,5 +1,6 @@
# Example to create a bios compatible gpt partition # Example to create a bios compatible gpt partition
{ disks ? [ "/dev/vdb" ], lib, ... }: { { disks ? [ "/dev/vdb" ], lib, ... }: {
disko.devices = {
disk = lib.genAttrs [ (lib.head disks) ] (device: { disk = lib.genAttrs [ (lib.head disks) ] (device: {
device = device; device = device;
type = "disk"; type = "disk";
@ -32,4 +33,5 @@
]; ];
}; };
}); });
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
vdb = { vdb = {
type = "disk"; type = "disk";
@ -62,5 +63,6 @@
}; };
}; };
}; };
};
} }

View file

@ -1,4 +1,5 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: { { disks ? [ "/dev/vdb" "/dev/vdc" ], ... }: {
disko.devices = {
disk = { disk = {
x = { x = {
type = "disk"; type = "disk";
@ -106,5 +107,6 @@
}; };
}; };
}; };
};
} }

View file

@ -22,8 +22,7 @@ let
(lib.attrNames (builtins.readDir ./.)) (lib.attrNames (builtins.readDir ./.))
); );
allTests = lib.genAttrs allTestFilenames (test: import (./. + "/${test}.nix") { inherit makeDiskoTest pkgs; }) // allTests = lib.genAttrs allTestFilenames (test: import (./. + "/${test}.nix") { inherit makeDiskoTest pkgs; }) // {
evalTest "lvm-luks-example" ../example/config.nix // {
standalone = (pkgs.nixos [ ../example/stand-alone/configuration.nix ]).config.system.build.toplevel; standalone = (pkgs.nixos [ ../example/stand-alone/configuration.nix ]).config.system.build.toplevel;
}; };
in in

View file

@ -30,16 +30,16 @@
tsp-mount = (tsp-generator.mountScript (import disko-config { disks = builtins.tail disks; inherit lib; })) pkgs; tsp-mount = (tsp-generator.mountScript (import disko-config { disks = builtins.tail disks; inherit lib; })) pkgs;
tsp-disko = (tsp-generator.zapCreateMountScript (import disko-config { disks = builtins.tail disks; inherit lib; })) pkgs; tsp-disko = (tsp-generator.zapCreateMountScript (import disko-config { disks = builtins.tail disks; inherit lib; })) pkgs;
tsp-config = tsp-generator.config (import disko-config { inherit disks; inherit lib; }); tsp-config = tsp-generator.config (import disko-config { inherit disks; inherit lib; });
num-disks = builtins.length (lib.attrNames (import disko-config { inherit lib; }).disk); num-disks = builtins.length (lib.attrNames (import disko-config { inherit lib; }).disko.devices.disk);
installed-system = { modulesPath, ... }: { installed-system = { modulesPath, ... }: {
imports = [ imports = [
(lib.optionalAttrs (testMode == "direct" || testMode == "cli") tsp-config) (lib.optionalAttrs (testMode == "direct" || testMode == "cli") tsp-config)
(lib.optionalAttrs (testMode == "module") { (lib.optionalAttrs (testMode == "module") {
imports = [ ../module.nix ]; disko.enableConfig = true;
disko = { imports = [
enableConfig = true; ../module.nix
devices = import disko-config { inherit disks lib; }; (import disko-config { inherit disks lib; })
}; ];
}) })
(modulesPath + "/testing/test-instrumentation.nix") (modulesPath + "/testing/test-instrumentation.nix")
(modulesPath + "/profiles/qemu-guest.nix") (modulesPath + "/profiles/qemu-guest.nix")
@ -77,11 +77,13 @@
nodes.machine = { config, pkgs, modulesPath, ... }: { nodes.machine = { config, pkgs, modulesPath, ... }: {
imports = [ imports = [
(lib.optionalAttrs (testMode == "module") { (lib.optionalAttrs (testMode == "module") {
imports = [ ../module.nix ]; imports = [
../module.nix
];
disko = { disko = {
enableConfig = false; enableConfig = false;
checkScripts = true; checkScripts = true;
devices = import disko-config { disks = builtins.tail disks; inherit lib; }; devices = (import disko-config { disks = builtins.tail disks; inherit lib; }).disko.devices;
}; };
}) })
(lib.optionalAttrs (testMode == "cli") { (lib.optionalAttrs (testMode == "cli") {