Merge pull request #162 from nix-community/examples-config

This commit is contained in:
Lassulus 2023-04-06 09:48:07 +02:00 committed by GitHub
commit 8891df7029
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 1170 additions and 1200 deletions

View file

@ -1,3 +1,11 @@
2023-03-22 2624af6
disk config now needs to be inside a disko.devices attrset always
2023-03-22 0577409
the extraArgs option in the luks type was renamed to extraFormatArgs
2023-02-14 6d630b8 2023-02-14 6d630b8
btrfs, btrfs_subvol filesystem and lvm_lv extraArgs are now lists btrfs, btrfs_subvol filesystem and lvm_lv extraArgs are now lists

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

@ -18,10 +18,9 @@ makeDiskoTest {
machine.succeed("mountpoint /ext4onzfs"); machine.succeed("mountpoint /ext4onzfs");
machine.succeed("mountpoint /ext4_on_lvm"); machine.succeed("mountpoint /ext4_on_lvm");
''; '';
enableOCR = true;
bootCommands = '' bootCommands = ''
machine.wait_for_text("[Pp]assphrase for") machine.wait_for_console_text("vda")
machine.send_chars("secretsecret\n") machine.send_console("secretsecret\n")
''; '';
extraConfig = { extraConfig = {
boot.kernelModules = [ "dm-raid" "dm-mirror" ]; boot.kernelModules = [ "dm-raid" "dm-mirror" ];

View file

@ -17,10 +17,9 @@ makeDiskoTest {
machine.succeed("mountpoint /ext4onzfs"); machine.succeed("mountpoint /ext4onzfs");
machine.succeed("mountpoint /ext4_on_lvm"); machine.succeed("mountpoint /ext4_on_lvm");
''; '';
enableOCR = true;
bootCommands = '' bootCommands = ''
machine.wait_for_text("[Pp]assphrase for") machine.wait_for_console_text("vda")
machine.send_chars("secretsecret\n") machine.send_console("secretsecret\n")
''; '';
extraConfig = { extraConfig = {
boot.kernelModules = [ "dm-raid" "dm-mirror" ]; boot.kernelModules = [ "dm-raid" "dm-mirror" ];

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")
@ -55,7 +55,7 @@
documentation.enable = false; documentation.enable = false;
hardware.enableAllFirmware = lib.mkForce false; hardware.enableAllFirmware = lib.mkForce false;
networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs
boot.kernelParams = lib.mkAfter [ "console=tty0" ]; # needed to have serial interaction during boot boot.kernelParams = lib.mkIf enableOCR [ "console=tty0" ]; # needed for OCR
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms
boot.consoleLogLevel = lib.mkForce 100; boot.consoleLogLevel = lib.mkForce 100;
@ -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") {

View file

@ -8,9 +8,8 @@ makeDiskoTest {
machine.succeed("cryptsetup isLuks /dev/vda2"); machine.succeed("cryptsetup isLuks /dev/vda2");
machine.succeed("mountpoint /home"); machine.succeed("mountpoint /home");
''; '';
enableOCR = true;
bootCommands = '' bootCommands = ''
machine.wait_for_text("[Pp]assphrase for") machine.wait_for_console_text("vda")
machine.send_chars("secretsecret\n") machine.send_console("secretsecret\n")
''; '';
} }

View file

@ -18,10 +18,9 @@ makeDiskoTest {
machine.succeed("mountpoint /ext4onzfs"); machine.succeed("mountpoint /ext4onzfs");
machine.succeed("mountpoint /ext4_on_lvm"); machine.succeed("mountpoint /ext4_on_lvm");
''; '';
enableOCR = true;
bootCommands = '' bootCommands = ''
machine.wait_for_text("[Pp]assphrase for") machine.wait_for_console_text("vda")
machine.send_chars("secretsecret\n") machine.send_console("secretsecret\n")
''; '';
extraConfig = { extraConfig = {
boot.kernelModules = [ "dm-raid" "dm-mirror" ]; boot.kernelModules = [ "dm-raid" "dm-mirror" ];

View file

@ -13,7 +13,7 @@ makeDiskoTest {
''; '';
enableOCR = true; enableOCR = true;
bootCommands = '' bootCommands = ''
machine.wait_for_text("(?:passphrase|key) for") machine.wait_for_text("passphrase for")
machine.send_chars("secretsecret\n") machine.send_chars("secretsecret\n")
''; '';
extraTestScript = '' extraTestScript = ''