an alternative to https://github.com/nix-community/disko/pull/1304
This introduces a new globals argument which can be used to pass in
different global state into the disko type. Makes the fix a little bit
cleaner.
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
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.
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.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* FIDO2 token enrollment for LUKS-encrypted root with optional automatic
recovery passphrase and new configuration options to control enrollment
behavior.
* **Examples**
* Added an example configuration demonstrating a FIDO2-protected disk
layout (ESP + encrypted root) with FIDO2 enrollment enabled.
* **Tests**
* Added automated test coverage for the FIDO2/LUKS workflow and a
test-mode option to simulate a hardware FIDO2 device.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.