Closes#1283
When building `vmWithDisko` via `extendModules`, the outer `module.nix`
is evaluated with `disko.enableConfig = true` (default), contributing
`swapDevices = cfg.devices._config.swapDevices or [ ]`. At the same
time, `lib/interactive-vm.nix` contributes `swapDevices =
cfg_.disko.devices._config.swapDevices or [ ]` via
`diskoBasedConfiguration`.
Because `swapDevices` in NixOS is a `listOf submodule`, definitions are
merged by concatenation. As a result, every swap partition in the disko
config appears twice in the VM's generated `/etc/fstab`:
```
# Swap devices.
/dev/mapper/dev-disk-byx2dpartlabel-diskx2dmainx2dencryptedSwap none swap defaults,pri=100
/dev/disk/by-partlabel/disk-main-plainSwap none swap defaults,discard
/dev/mapper/dev-disk-byx2dpartlabel-diskx2dmainx2dencryptedSwap none swap defaults,pri=100
/dev/disk/by-partlabel/disk-main-plainSwap none swap defaults,discard
```
During boot, `systemd-fstab-generator` fails with exit status 1 because
duplicate `.swap` unit files cannot be created.
### Fix
Setting `disko.enableConfig = false;` in `lib/interactive-vm.nix`
prevents `module.nix` from injecting the unmapped host disk
configurations into the VM variant. `interactive-vm.nix`'s
`diskoBasedConfiguration` already supplies the properly QEMU-mapped
filesystem, boot, and swap configurations.
### Verification
Evaluated on `example/swap.nix`:
- **Before**:
`vmSwapDevices` has 4 entries (2 pairs of identical devices).
- **After**:
`vmSwapDevices` has 2 unique entries.
`fileSystems` and `vmWithDisko` script remain intact and functional.
Added `tests/vm-with-disko-swap.nix` ensuring that
`vmVariantWithDisko.swapDevices` has no duplicate devices. Checked with
`nix build .#checks.x86_64-linux.vm-with-disko-swap` and verified
`treefmt` formatting.
`randomEncryption` on the `swap` partition type was typed as a plain
`bool`, which only ever built `{ enable = ...; allowDiscards = ...; }`
for NixOS's `swapDevices.*.randomEncryption` submodule, so there was no
way to reach its other options (`cipher`, `sectorSize`, etc.), as noted
in the issue.
Per the maintainer's suggestion in the issue thread, this widens the
type to `lib.types.either lib.types.bool (lib.types.attrsOf
lib.types.anything)`:
- A plain `true`/`false` keeps the exact old behavior (backwards
compatible: verified both existing in-repo usages, `example/swap.nix`
and `tests/mdadm-btrfs-wipe.nix`, still use a bare bool and are
unaffected).
- An attrset enables encryption by default (`enable = true`, overridable
by setting `enable` in the attrset), and is merged over the
`enable`/`allowDiscards` defaults so any extra NixOS `randomEncryption`
options (cipher, sectorSize, ...) pass straight through.
Also introduced a `randomEncryptionEnabled` derived boolean
(`config.randomEncryption` when it's a bool, else `.enable or true`)
since the rest of the module (`_create`/`_mount`/`_unmount`) branches on
whether random encryption is active at all, and `!` doesn't work on an
attrset.
I don't have a local Nix environment to run `nix flake check` / the test
suite against this change, so flagging that so a maintainer/CI can
verify directly.
Fixes#438
randomEncryption was typed as a plain bool, which just passed
enable/allowDiscards down to NixOS's swapDevices.*.randomEncryption
submodule, with no way to reach its other options (cipher, sectorSize,
etc).
Widen the type to bool or an attrset. A bool keeps the old behavior
unchanged. An attrset enables encryption by default (overridable via
its own enable key) and is merged over the enable/allowDiscards
defaults, so extra NixOS randomEncryption options pass through.
Fixes#438
Fixes#1245
The "upgrade guide" link in `docs/HowTo.md` pointed to
`github.com/JillThornhill/disko/blob/master/docs/upgrade-guide.md` (a
fork) instead of the canonical `docs/upgrade-guide.md`, which already
exists in this repo. Changed it to a relative link.
I've been using Disko to both configure filesystems on my phone and
create the initial images that are flashed. The only problem was that
the installed config would have its device names overwritten. This PR
adds an option to chose if the virtual/cleaned device names should be
installed or not. They're still always used during partitioning.
For those curious, here's an example phone Disko config:
<details>
<summary><i>expand me</i></summary>
```nix
# This config is NOT meant to be used with the Disko CLI. It should only be used with
# the Disko image builder. It represents the boot and root images that will be
# created with Disko image builder and flashed to the phone. NixOS is then able to find
# the partitions using /dev/disk/by-label based on this config.
{
disko.devices.disk = {
boot = let
label = "nixos-boot";
in {
type = "disk";
device = "/dev/disk/by-label/${label}";
imageName = label;
# vfat can't be auto-expanded by NixOS, so the initial image size must be
# the final desired size.
imageSize = "2G";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
mountOptions = [
# See https://github.com/nix-community/disko/issues/527
"umask=0077"
# Continuous Discard/trim For SSDs. This partition is small and not written
# to a ton, so I doubt performance will be an issue.
"discard"
];
extraArgs = [
"-n"
label
# Sector size.
"-S"
"4096"
];
};
};
root = let
label = "nixos-root";
in {
type = "disk";
device = "/dev/disk/by-label/${label}";
imageName = label;
imageSize = "5G";
content = {
type = "luks";
name = "crypt";
settings = {
# Enable discard/TRIM support. fstrim shouldn't be needed, b/c BTRFS
# discard=async is default.
# See https://wiki.archlinux.org/title/Dm-crypt/Specialties#Discard/TRIM_support_for_solid_state_drives_(SSD)
allowDiscards = true;
# Improve SSD performance.
# See https://wiki.archlinux.org/title/Dm-crypt/Specialties#Disable_workqueue_for_increased_solid_state_drive_(SSD)_performance
bypassWorkqueues = true;
};
extraFormatArgs = [
"--label"
label
"--sector-size"
"4096"
];
# Just for initial encryption in the VM.
passwordFile = "/tmp/nixos-root.key";
content = {
type = "btrfs";
extraArgs = [
"--sectorsize"
"4096"
"--force" # Force overrite existing partition
];
subvolumes = {
...
};
};
};
};
};
}
```
</details>
This PR addresses the handling of multiple LUKS partitions on the same
disk and adds a corresponding test case.
- **Fix:** The core logic for handling multiple LUKS partitions on a
single disk was improved to ensure correct partition mapping and
unlocking. (Implemented using a custom pi agent and the **mimo-v2.5**
model).
- **Tests:** Added `luks-multiple` test to verify the behavior of
multiple LUKS partitions on the same disk. (Tests written by me).
Reproduces and fixes#1263
## Summary
Detached LUKS headers were not handled correctly by the `_create`
idempotence check in `lib/types/luks.nix`.
The old logic still probed `config.device`. For detached-header setups,
that meant rerun could mis-detect an already-initialized LUKS device and
fall back into luksFormat.
## Fix
Probe the actual LUKS header target instead of the payload device, and
make the create guard depend only on `cryptsetup isLuks` for that
target.
## Test
Add a detached-header regression test that exercises the direct-mode
idempotence path.
Closes#1298
Supersedes #1277
Supersedes #1299
### Problem
Following nixpkgs commit 68d32ed6cbd986a84582b88780168d22adebd6fc (PR
#530764), `pkgs.vmTools` split the boot kernel image (`kernel`) from the
initrd modules tree (`kernelModules`). When building disk images or
`vmWithDisko` against recent `nixpkgs-unstable`, `make-disk-image.nix`
fails during evaluation:
```
error: vmTools: the `kernel` argument (kernel-modules) has no
`target` attribute, so the kernel image filename cannot be determined.
If you are passing a module tree (e.g. from `pkgs.aggregateModules`) to
make extra modules available, pass it via `kernelModules` instead and
keep `kernel` pointing at a real kernel derivation.
```
Previous attempts (#1277, #1299) attempted an unconditional pass of
`kernelModules`, but this fails on older nixpkgs (including disko's
current locked nixpkgs) with `unexpected argument 'kernelModules'`.
### Fix
1. Added helper `diskoLib.vmToolsSupportsKernelModules`:
```nix
vmToolsSupportsKernelModules =
pkgs:
(pkgs.vmTools.override.__functionArgs or (
if lib.isFunction pkgs.vmTools.override then builtins.functionArgs
pkgs.vmTools.override else { }
)
) ? kernelModules;
```
2. In `lib/make-disk-image.nix`, conditionally pass `kernel` and
`kernelModules` if supported by `pkgs.vmTools`, else fall back to
`kernel = modulesTree`.
This ensures full backward compatibility across nixpkgs versions without
requiring a `flake.lock` bump.
### Verification
- **Modern nixpkgs**: Evaluated `nix eval --override-input nixpkgs
nixpkgs .#checks.x86_64-linux.make-disk-image.drvPath` — successfully
produces derivation (previously failed with the error above).
- **Locked nixpkgs**: Evaluated `nix eval
.#checks.x86_64-linux.make-disk-image.drvPath` — passes cleanly.
- **Checks & formatting**: `nix build .#checks.x86_64-linux.treefmt`,
`checkJqSyntax`, and `jsonTypes` all pass cleanly.
When building `vmWithDisko` via `extendModules`, the outer `module.nix` is
evaluated with `disko.enableConfig = true` (default), contributing
`swapDevices = cfg.devices._config.swapDevices or [ ]`. At the same time,
`interactive-vm.nix` contributes `cfg_.disko.devices._config.swapDevices`
via `diskoBasedConfiguration`.
Because `swapDevices` in NixOS is a `listOf submodule`, definitions are
merged by concatenation, causing every swap device from the disko config to
appear twice in the generated fstab. During boot, `systemd-fstab-generator`
then fails with exit status 1 due to conflicting generated swap units.
Setting `disko.enableConfig = false` in `interactive-vm.nix` prevents the outer
module from injecting unmapped host disko configurations, while allowing
`interactive-vm.nix` to supply the QEMU-mapped devices cleanly.
Closes#1283
Signed-off-by: Ravi Arnan <raviarnankeren@gmail.com>
askPassword's mismatch branch does exit 1, which kills the whole format
script instead of just returning from the function. The per-device
_create block runs in a subshell, and the top-level script has set
-efux, so a subshell that exits nonzero takes the whole thing down with
it.
The retry loop this was supposed to feed into never gets a chance to
run:
```
until askPassword; do
echo "Passwords did not match, please try again."
done
```
`askPassword` never returns from the mismatch branch, so until never
sees a nonzero return, it just watches the whole script die. Type your
password wrong once during formatting and disko doesn't ask again, it
aborts.
Same bug and same fix as #1206 (exit 1 -> return 1), which has been
sitting unreviewed for a while. Credit to @Cyclic4179 for finding it
first.
While in there, I also capped the retry loop at 3 attempts. As written
it would otherwise retry forever, and a script that just hangs asking
for input with no way out isn't much better than one that silently dies.
3 also isn't arbitrary: it's what tries= defaults to in crypttab(5) for
unlocking this same kind of volume at boot, so the format-time prompt
now matches that.
Added tests/luks-password-mismatch.nix covering both paths:
**a mismatched pair followed by a matching one:** format should succeed,
and the retry message should actually show up in the output.
**three mismatched pairs in a row:** format should fail, the disk should
not end up as valid LUKS, and the "too many attempts" message should be
there instead of a silent exit code 1.
Both fail on current master and pass with this change.
Adds missing testscript code to execute unmount scripts. With this
change all test derivations of the Disko examples verify proper
unmounting behaviour.
Part of stacked pull requests to enable unmount testing. This is PR 4
out of 4. Stacks on top of #1149.
Only the last commit applies to this PR.
See #1140 for ealier discussion.
Using FIDO2 automatically generates a temporary password for setting up LUKS
which is not available on subsequent runs of the script causing the format
script to crash with `password: unbound variable`.
`bcachefs-tools`: `v1.33.1` cannot read the superblock from a mounted
device. The kernel returns EBUSY whenever
the filesystem is already mounted, so every `bcachefs show-super
/dev/vd*` assertion in this test now fails on the booted system.
`boot.initrd.preDeviceCommands` only runs in the script-based stage 1
and is gated on `!config.boot.initrd.systemd.enable`. Tests that
enable systemd initrd therefore never get `/tmp/secret.key` staged,
and disko's encrypted-filesystem mount hooks fail to find the
configured `passwordFile`.
The nixpkgs test driver was refactored: `driver.machines` is now a
computed property that returns a fresh sorted list combining
`driver.machines_qemu` and `driver.machines_nspawn`. Appending to the
property therefore mutates only the temporary returned list, so the
machines created via `create_test_machine` are no longer registered
with the driver.