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 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
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.
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>
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`.
`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.
This introduces what has been described in
https://0pointer.net/blog/unlocking-luks2-volumes-with-tpm2-fido2-pkcs11-security-hardware-on-systemd-248.html.
That is: support for FIDO2 and recovery passphrases and their boot
mechanism.
Testing is hard right now because of canokey-qemu being broken and U2F
is not a valid replacement for FIDO2…
I tried to keep as much as possible the previous behavior and make it
possible to mix FIDO2 and normal passphrases or key files without any
problem.
PIV support is out of scope for this change but can easily be added.
Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
Use the `fsBefore` function from NixOS to sort the file-systems.
The function originates from `nixpkgs/nixos/lib/utils.nix`, but the file
is a lambda that requires more arguments than we have available.
Use the `fsBefore` function from NixOS to sort the file-systems.
The function originates from `nixpkgs/nixos/lib/utils.nix`, but the file
is a lambda that requires more arguments than we have available.
The device of the bind mounts is incorrectly taken verbatim while making
the image. The root hierarchy is mounted at `config.rootMountDir`
(`/mnt` by default) during the process, and this commit prepends that
path to the `device` in these cases.
Nix refuses to operate when the store path or its parent directories
contain symlinks. On some systems /mnt can be a symlink, causing
disko-install to fail with:
error: the path '"/mnt"' is a symlink; this is not allowed for the
Nix store and its parent directories
Use realpath to resolve symlinks before passing the mount point to
nix-store and nixos-install commands.
Allow users to override the automatic thread count detection for nix
store copying. The default "auto" behavior remains unchanged (min of
nproc and 8), but users can now specify a fixed thread count for
environments where the automatic detection isn't optimal.