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.