Compare commits

...
Author SHA1 Message Date
Jörg Thalheim ff442f5d14 release: v1.12.0 2025-05-08 20:14:14 +02:00
Jörg Thalheim 6bb82b77ce luks: drop unnecessary subshell 2025-05-08 09:13:14 +00:00
Philipp Schmitt 06fb9283ca Unlock luks devices once and only once 2025-05-08 09:13:14 +00:00
Philipp Schmitt 69265ccde5 Fix _destroy 2025-05-08 09:13:14 +00:00
Philipp Schmitt cbc1f418d0 Improve luks device detection 2025-05-08 09:13:14 +00:00
Philipp Schmitt aa5d53aee8 Add disk.destroy option 2025-05-08 09:13:14 +00:00
Jörg Thalheim c7e0b00007 diskoImagesScript: unset NIX_REMOTE
for some reason this is set and breaks running disko outside of the vm
2025-05-08 09:07:26 +00:00
Jörg Thalheim aba0ae38df fix evaluation of disko-images example 2025-05-08 09:07:26 +00:00
Jörg Thalheim b5cfd59e9a fix documentation on booting disko inside a VM
fixes https://github.com/nix-community/disko/issues/1018
2025-05-08 09:07:26 +00:00
github-actions[bot] c4fe2d108b flake.lock: Update
Flake lock file updates:

• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/f21e4546e3ede7ae34d12a84602a22246b31f7e0?narHash=sha256-thYTdWqCRipwPRxWiTiH1vusLuAy0okjOyzRx4hLWh4%3D' (2025-05-03)
  → 'github:NixOS/nixpkgs/b3582c75c7f21ce0b429898980eddbbf05c68e55?narHash=sha256-FshoQvr6Aor5SnORVvh/ZdJ1Sa2U4ZrIMwKBX5k2wu0%3D' (2025-05-07)
2025-05-08 02:09:15 +00:00
nothingnesses 78d6a1365c Use a hard-coded UUID for the failing example to get it to work 2025-05-07 17:27:41 +00:00
Kyle Petryszak 8d78963819 fix: addresses: https://github.com/koverstreet/bcachefs/issues/812 2025-05-07 17:27:41 +00:00
github-actions[bot] b5d1320ebc flake.lock: Update
Flake lock file updates:

• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/ebe4301cbd8f81c4f8d3244b3632338bbeb6d49c?narHash=sha256-5RJTdUHDmj12Qsv7XOhuospjAjATNiTMElplWnJE9Hs%3D' (2025-04-17)
  → 'github:NixOS/nixpkgs/f21e4546e3ede7ae34d12a84602a22246b31f7e0?narHash=sha256-thYTdWqCRipwPRxWiTiH1vusLuAy0okjOyzRx4hLWh4%3D' (2025-05-03)
2025-05-05 02:11:54 +00:00
nothingnesses 7b63642358 Make bcachefs subvolumes boot-time mount tests pass + more tests 2025-05-04 20:24:55 +00:00
11 changed files with 276 additions and 203 deletions

No files matched your search

+3
View File
@@ -4,3 +4,6 @@ result-*
# Created by the NixOS interactive test driver
.nixos-test-history
# Created by `:bl` in `nix repl`
repl-result-out
+74 -8
View File
@@ -1,11 +1,9 @@
# Generating Disk Images with Secrets Included using Disko
Using Disko on NixOS allows you to efficiently create `.raw` VM images from a
system configuration. The generated image can be used as a VM or directly
written to a physical drive to create a bootable disk. Follow the steps below to
generate disk images:
Using Disko on NixOS allows you to efficiently create `.raw` disk images from a system configuration.
Follow the steps below to generate disk images:
## Generating the `.raw` VM Image
## Generating the `.raw` Image
1. **Create a NixOS configuration that includes the disko and the disk
configuration of your choice**
@@ -24,16 +22,29 @@ In the this example we create a flake containing a nixos configuration for
disko.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, disko, nixpkgs }: {
outputs = { self, disko, nixpkgs, ... }: {
nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
# You can get this file from here: https://github.com/nix-community/disko/blob/master/example/simple-efi.nix
./simple-efi.nix
disko.nixosModules.disko
({ config, ... }: {
({ config, modulesPath, ... }: {
imports = [
# include this line to boot to boot the image inside a VM
# "${modulesPath}/profiles/qemu-guest.nix"
# On other hardware, you may need a hardware-configuration.nix as generated by nixos-generate-config or use nixos-facter (https://github.com/numtide/nixos-facter)
# ./hardware-configuration.nix
];
# Optional. Useful for testing
# users.users.root.initialPassword = "root";
boot.loader.grub.efiSupport = lib.mkDefault true;
boot.loader.grub.efiInstallAsRemovable = lib.mkDefault true;
# shut up state version warning
system.stateVersion = config.system.nixos.version;
system.stateVersion = config.system.nixos.release;
# Adjust this to your liking.
# WARNING: if you set a too low value the image might be not big enough to contain the nixos installation
disko.devices.disk.main.imageSize = "10G";
@@ -141,3 +152,58 @@ In the this example we create a flake containing a nixos configuration for
By following these instructions and understanding the process, you can smoothly
generate disk images with Disko for your NixOS system configurations.
## Test the image inside a VM
Make sure you uncommented the `"${modulesPath}/profiles/qemu-guest.nix"` import in the example above!
Write the following script to `qemu.nix`. Note that it expects an UEFI compatible image!
```
with import <nixpkgs> {};
writeShellApplication {
name = "test-image";
runtimeInputs = [ qemu ];
text = ''
if [ -z "$1" ]; then
echo "Usage: $0 <path-to-boot-image>"
exit 1
fi
tmpFile=$(mktemp /tmp/test-image.XXXXXX)
trap 'rm -f $tmpFile' EXIT
cp "$1" "$tmpFile"
qemu-system-x86_64 \
-enable-kvm \
-m 2G \
-cpu max \
-smp 2 \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=net0 \
-drive if=pflash,format=raw,readonly=on,file=${OVMF.firmware} \
-drive if=pflash,format=raw,readonly=on,file=${OVMF.variables} \
-drive "if=virtio,format=raw,file=$tmpFile"
'';
}
```
This command will run a qemu vm with your image:
```
$(nix-build ./qemu.nix)/bin/test-image ./main.raw
```
Replace `main.raw` with the image that you build!
Tip: You can customize memory (`-m` flag) or cpu (`-smp` flag) available to the VM to your liking.
## Build the image inside the nix sandbox
Instead of the diskoImagesScript, we can also build the image inside the nix store.
This approach is slower because it requires copying the image after the build and
we also don't have a secure way to embed secrets this way.
```
nix build .#nixosConfigurations.myhost.config.system.build.diskoImages
ls -a ./result/main.raw
```
+42 -141
View File
@@ -23,7 +23,7 @@
content = {
type = "bcachefs";
# This refers to a filesystem in the `bcachefs_filesystems` attrset below.
filesystem = "unmounted_subvolumes_in_multi";
filesystem = "mounted_subvolumes_in_multi";
label = "group_a.vdb2";
extraFormatArgs = [
"--discard"
@@ -44,7 +44,7 @@
size = "100%";
content = {
type = "bcachefs";
filesystem = "unmounted_subvolumes_in_multi";
filesystem = "mounted_subvolumes_in_multi";
label = "group_a.vdc1";
extraFormatArgs = [
"--discard"
@@ -65,7 +65,7 @@
size = "100%";
content = {
type = "bcachefs";
filesystem = "unmounted_subvolumes_in_multi";
filesystem = "mounted_subvolumes_in_multi";
label = "group_b.vdd1";
extraFormatArgs = [
"--force"
@@ -79,87 +79,6 @@
vde = {
device = "/dev/vde";
type = "disk";
content = {
type = "gpt";
partitions = {
vde1 = {
size = "100%";
content = {
type = "bcachefs";
filesystem = "mounted_subvolumes_in_multi";
label = "group_a.vde1";
extraFormatArgs = [
"--discard"
];
};
};
};
};
};
vdf = {
device = "/dev/vdf";
type = "disk";
content = {
type = "gpt";
partitions = {
vdf1 = {
size = "100%";
content = {
type = "bcachefs";
filesystem = "mounted_subvolumes_in_multi";
label = "group_a.vdf1";
extraFormatArgs = [
"--discard"
];
};
};
};
};
};
vdg = {
device = "/dev/vdg";
type = "disk";
content = {
type = "gpt";
partitions = {
vdd1 = {
size = "100%";
content = {
type = "bcachefs";
filesystem = "mounted_subvolumes_in_multi";
label = "group_b.vdg1";
extraFormatArgs = [
"--force"
];
};
};
};
};
};
vdh = {
device = "/dev/vdh";
type = "disk";
content = {
type = "gpt";
partitions = {
vdd1 = {
size = "100%";
content = {
type = "bcachefs";
filesystem = "no_reliance_on_external_subvolume";
label = "group_a.vdh1";
};
};
};
};
};
vdi = {
device = "/dev/vdi";
type = "disk";
content = {
type = "gpt";
partitions = {
@@ -168,7 +87,7 @@
content = {
type = "bcachefs";
filesystem = "relies_on_external_subvolume";
label = "group_a.vdi1";
label = "group_a.vde1";
};
};
};
@@ -177,72 +96,54 @@
};
bcachefs_filesystems = {
# Example showing unmounted subvolumes in a multi-disk configuration.
unmounted_subvolumes_in_multi = {
# Example showing mounted subvolumes in a multi-disk configuration.
mounted_subvolumes_in_multi = {
type = "bcachefs_filesystem";
passwordFile = "/tmp/secret.key";
extraFormatArgs = [
"--compression=lz4"
"--background_compression=lz4"
];
subvolumes = {
# Subvolume name is different from mountpoint.
"subvolumes/root" = {
mountpoint = "/";
mountOptions = [
"verbose"
];
};
# Subvolume name is the same as the mountpoint.
"subvolumes/home" = {
mountpoint = "/home";
};
# Nested subvolume doesn't need a mountpoint as its parent is mounted.
"subvolumes/home/user" = {
};
# Parent is not mounted so the mountpoint must be set.
"subvolumes/nix" = {
mountpoint = "/nix";
};
# This subvolume will be created but not mounted.
"subvolumes/test" = {
};
};
};
# Example showing a bcachefs filesystem without subvolumes,
# which relies on a subvolume in another filesystem being mounted
# and uses a hard-coded UUID.
relies_on_external_subvolume = {
type = "bcachefs_filesystem";
mountpoint = "/home/Documents";
extraFormatArgs = [
"--compression=lz4"
"--background_compression=lz4"
];
mountOptions = [
"verbose"
];
mountpoint = "/";
subvolumes = {
"subvolumes/rootfs" = { };
"subvolumes/home" = { };
"subvolumes/home/user" = { };
"subvolumes/nix" = { };
"subvolumes/test" = { };
};
uuid = "64e50034-ebe2-eaf8-1f93-cf56266a8d86";
};
# # Example showing mounted subvolumes in a multi-disk configuration (not yet working).
# mounted_subvolumes_in_multi = {
# type = "bcachefs_filesystem";
# passwordFile = "/tmp/secret.key";
# extraFormatArgs = [
# "--compression=lz4"
# "--background_compression=lz4"
# ];
# mountOptions = [
# "verbose"
# ];
# subvolumes = {
# # Subvolume name is different from mountpoint
# "foo" = {
# mountpoint = "/bar";
# };
# # Subvolume name is the same as the mountpoint
# "home" = {
# mountpoint = "/home";
# };
# # Sub(sub)volume doesn't need a mountpoint as its parent is mounted
# "home/user" = {
# };
# # Parent is not mounted so the mountpoint must be set
# "nix" = {
# mountpoint = "/nix";
# };
# # This subvolume will be created but not mounted
# "test" = {
# };
# };
# };
# Example showing another bcachefs filesystem.
no_reliance_on_external_subvolume = {
type = "bcachefs_filesystem";
mountpoint = "/sometestdir";
};
# # Example showing another bcachefs filesystem that relies on a subvolume
# # in another filesystem being mounted (not yet working).
# relies_on_external_subvolume = {
# type = "bcachefs_filesystem";
# mountpoint = "/home/somedir/vdf1";
# };
};
};
}
Generated
+3 -3
View File
@@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1744868846,
"narHash": "sha256-5RJTdUHDmj12Qsv7XOhuospjAjATNiTMElplWnJE9Hs=",
"lastModified": 1746576598,
"narHash": "sha256-FshoQvr6Aor5SnORVvh/ZdJ1Sa2U4ZrIMwKBX5k2wu0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ebe4301cbd8f81c4f8d3244b3632338bbeb6d49c",
"rev": "b3582c75c7f21ce0b429898980eddbbf05c68e55",
"type": "github"
},
"original": {
+10 -3
View File
@@ -875,9 +875,15 @@ let
umount -Rv "${rootMountPoint}" || :
# shellcheck disable=SC2043,2041
for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do
for dev in ${
toString (
lib.catAttrs "device" (lib.attrValues (lib.filterAttrs (name: disk: disk.destroy) devices.disk))
)
};
do
$BASH ${../disk-deactivate}/disk-deactivate "$dev"
done
'';
};
_destroy = lib.mkOption {
@@ -888,7 +894,8 @@ let
'';
default =
let
selectedDisks = lib.escapeShellArgs (lib.catAttrs "device" (lib.attrValues devices.disk));
disksToWipe = lib.attrValues (lib.filterAttrs (name: disk: disk.destroy) devices.disk);
selectedDisks = lib.escapeShellArgs (lib.catAttrs "device" disksToWipe);
in
''
if [ "$1" != "--yes-wipe-all-disks" ]; then
@@ -913,7 +920,7 @@ let
umount -Rv "${rootMountPoint}" || :
# shellcheck disable=SC2043,2041
for dev in ${selectedDisks}; do
for dev in ${toString (lib.catAttrs "device" disksToWipe)}; do
$BASH ${../disk-deactivate}/disk-deactivate "$dev"
done
'';
+1
View File
@@ -136,6 +136,7 @@ let
installer = lib.optionalString cfg.copyNixStore ''
${binfmtSetup}
unset NIX_REMOTE
# populate nix db, so nixos-install doesn't complain
export NIX_STATE_DIR=${systemToInstall.config.disko.rootMountPoint}/nix/var/nix
nix-store --load-db < "${closureInfo}/registration"
+16 -8
View File
@@ -113,10 +113,7 @@
mountpoint = lib.mkOption {
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
default = null;
description = ''
Path to mount the subvolume to.
DO NOT USE. Currently not working.
'';
description = "Path to mount the subvolume to.";
example = "/";
};
};
@@ -126,7 +123,19 @@
default = { };
description = "List of subvolumes to define.";
example = {
"subvolumes/home" = { };
"subvolumes/root" = {
mountpoint = "/";
extraFormatArgs = [
"--compression=lz4"
"--background_compression=lz4"
];
mountOptions = [
"verbose"
];
};
"subvolumes/home" = {
mountpoint = "/home";
};
};
};
_parent = lib.mkOption {
@@ -291,11 +300,10 @@
_config = lib.mkOption {
internal = true;
readOnly = true;
# @todo Check that this implementation is correct:
default =
(lib.optional (config.mountpoint != null) {
fileSystems.${config.mountpoint} = {
device = "UUID=${config.uuid}";
device = "/dev/disk/by-uuid/${config.uuid}";
fsType = "bcachefs";
options = lib.unique ([ "X-mount.mkdir" ] ++ config.mountOptions);
neededForBoot = true;
@@ -303,7 +311,7 @@
})
++ (map (subvolume: {
fileSystems.${subvolume.mountpoint} = {
device = "UUID=${config.uuid}";
device = "/dev/disk/by-uuid/${config.uuid}";
fsType = "bcachefs";
options = lib.unique (
[
+5
View File
@@ -22,6 +22,11 @@
type = diskoLib.optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile
description = "Device path";
};
destroy = lib.mkOption {
type = lib.types.bool;
default = true;
description = "If false, disko will not wipe or destroy this disk's contents during the destroy stage";
};
imageName = lib.mkOption {
type = lib.types.str;
default = config.name;
+11 -7
View File
@@ -130,7 +130,7 @@ in
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
if ! blkid "${config.device}" >/dev/null || ! (blkid "${config.device}" -o export | grep -q '^TYPE='); then
if ! blkid "${config.device}" >/dev/null || ! cryptsetup isLuks "${config.device}"; then
${lib.optionalString config.askPassword ''
promptSecret() {
prompt=$1
@@ -158,13 +158,17 @@ in
done
''}
cryptsetup -q luksFormat "${config.device}" ${toString config.extraFormatArgs} ${keyFileArgs}
${cryptsetupOpen} --persistent
${toString (
lib.forEach config.additionalKeyFiles (keyFile: ''
cryptsetup luksAddKey "${config.device}" ${keyFile} ${keyFileArgs}
'')
)}
fi
if ! cryptsetup status "${config.name}" >/dev/null; then
${cryptsetupOpen} --persistent
fi
${toString (
lib.forEach config.additionalKeyFiles (keyFile: ''
cryptsetup luksAddKey "${config.device}" ${keyFile} ${keyFileArgs}
'')
)}
${lib.optionalString (config.content != null) config.content._create}
'';
};
+110 -29
View File
@@ -7,39 +7,120 @@ diskoLib.testLib.makeDiskoTest {
name = "bcachefs";
disko-config = ../example/bcachefs.nix;
enableOCR = true;
bootCommands = ''
machine.wait_for_text("enter passphrase for /");
machine.send_chars("secretsecret\n");
machine.wait_for_text("enter passphrase for /home");
machine.send_chars("secretsecret\n");
machine.wait_for_text("enter passphrase for /nix");
machine.send_chars("secretsecret\n");
'';
extraSystemConfig = {
environment.systemPackages = [
pkgs.jq
];
};
extraTestScript = ''
machine.succeed("mountpoint /");
# @todo Verify all devices are part of the filesystem.
# @todo Check device labels and group assignments.
# Verify mount options were applied.
machine.succeed("mount | grep ' / ' | grep -q 'compression=lz4'");
machine.succeed("mount | grep ' / ' | grep -q 'background_compression=lz4'");
# @todo Verify mountpoint dependency order was respected.
# @todo Add tests for subvolumes.
# Print debug information.
machine.succeed("ls -la /subvolumes >&2");
machine.succeed("lsblk >&2");
machine.succeed("lsblk -f >&2");
machine.succeed("mount >&2");
'';
# extraSystemConfig = { pkgs, ... }: {
# # @todo Do we need to add any attributes here?
# boot = {
# supportedFilesystems = [ "bcachefs" ];
# initrd = {
# supportedFilesystems = [ "bcachefs" ];
# # systemd.enable = false;
# };
# };
# environment.systemPackages = [
# pkgs.bcachefs-tools
# pkgs.util-linux
# ];
# };
# extraInstallerConfig = {
# # @todo Do we need to add any attributes here?
# };
bootCommands = ''
machine.wait_for_text("enter passphrase for");
machine.send_chars("secretsecret\n");
machine.succeed("bcachefs show-super /dev/vda2 >&2");
machine.succeed("bcachefs show-super /dev/vdd1 >&2");
machine.succeed("findmnt --json >&2");
# Verify subvolume structure.
machine.succeed("test -d /subvolumes/root");
machine.succeed("test -d /subvolumes/home");
machine.succeed("test -d /subvolumes/home/user");
machine.succeed("test -d /subvolumes/nix");
machine.succeed("test -d /subvolumes/test");
machine.fail("test -d /subvolumes/non-existent");
# Verify existence of mountpoints.
machine.succeed("mountpoint /");
machine.succeed("mountpoint /home");
machine.succeed("mountpoint /nix");
machine.succeed("mountpoint /home/Documents");
machine.fail("mountpoint /non-existent");
# Verify device membership and labels.
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Devices:' | grep -q '3'");
machine.succeed("bcachefs show-super /dev/vdd1 | grep 'Devices:' | grep -q '1'");
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'vdb2'");
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'vdc1'");
machine.succeed("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'vdd1'");
machine.succeed("bcachefs show-super /dev/vdd1 | grep 'Label:' | grep -q 'vde1'");
machine.fail("bcachefs show-super /dev/vda2 | grep 'Label:' | grep -q 'non-existent'");
# @todo Verify format arguments.
# Verify mount options from configuration.
machine.succeed("""
findmnt --json \
| jq -e ' \
.filesystems[] \
| select(.target == "/") \
| .options \
| split(",") \
| contains(["verbose", "compression=lz4", "background_compression=lz4"]) \
'
""");
machine.succeed("""
findmnt --json \
| jq -e ' \
.filesystems[] \
| .. \
| select(.target? == "/home/Documents") \
| .options \
| split(",") \
| contains(["verbose"]) \
'
""");
machine.fail("""
findmnt --json \
| jq -e ' \
.filesystems[] \
| select(.target == "/") \
| .options \
| split(",") \
| contains(["non-existent"]) \
'
""");
# Verify device composition of filesystems.
machine.succeed("""
findmnt --json \
| jq -e ' \
.filesystems[] \
| select(.target == "/") \
| .source | split(":") \
| contains(["/dev/vda2", "/dev/vdb1", "/dev/vdc1"]) \
'
""");
machine.succeed("""
findmnt --json \
| jq -e ' \
.filesystems[] \
| .. \
| select(.target? == "/home/Documents") \
| .source \
| contains("/dev/disk/by-uuid/64e50034-ebe2-eaf8-1f93-cf56266a8d86") \
'
""");
machine.fail("""
findmnt --json \
| jq -e ' \
.filesystems[] \
| select(.target == "/") \
| .source | split(":") \
| contains(["/dev/non-existent"]) \
'
""");
'';
}
+1 -4
View File
@@ -1,4 +1 @@
{
version = "1.10.0";
released = false;
}
{ version = "1.12.0"; released = true; }