mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 13:03:28 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad5e8bd14d | ||
|
|
007625baaa | ||
|
|
cf185d687f | ||
|
|
0d11aa8d64 | ||
|
|
337abec03f | ||
|
|
3704ee3926 | ||
|
|
f61c7d28d5 | ||
|
|
05f0e3fd56 | ||
|
|
0c9b003a48 | ||
|
|
c54e7e2b2b | ||
|
|
6a423861cd | ||
|
|
5c57c06779 | ||
|
|
561579a631 | ||
|
|
56f6d3d4af | ||
|
|
3cb3775f9b | ||
|
|
1f76b318aa | ||
|
|
2ca4b06bdf | ||
|
|
bde7dd352c | ||
|
|
a13f36255c | ||
|
|
23d308f005 | ||
|
|
590245271a | ||
|
|
1ae1f57dad | ||
|
|
d8a4377cd8 | ||
|
|
0661bb800c | ||
|
|
4d81082b2c | ||
|
|
c13d481b78 | ||
|
|
810eccbad2 | ||
|
|
2cb47338ec | ||
|
|
d07de570ba | ||
|
|
54274cc65f | ||
|
|
15b52c3c8a | ||
|
|
c49bb95ac8 | ||
|
|
2e9b88f02e | ||
|
|
f67ba65528 | ||
|
|
f7424625dc | ||
|
|
c12719812d | ||
|
|
5a2dc95464 | ||
|
|
9fcdf3375e | ||
|
|
0033adc6e3 | ||
|
|
d0b4408eaf | ||
|
|
38af7137e0 | ||
|
|
2a561be6b5 | ||
|
|
036d480ae8 | ||
|
|
f78b6498f6 | ||
|
|
b4afc8bab8 | ||
|
|
c38f369ee4 | ||
|
|
fa5db12d76 | ||
|
|
be305848c6 | ||
|
|
12d0c39ed3 | ||
|
|
acd2cbdb31 | ||
|
|
11f4ed1041 | ||
|
|
f0a3425a7b | ||
|
|
8a9e89d466 | ||
|
|
aef9a509db | ||
|
|
59f915b45a | ||
|
|
f772d2045e | ||
|
|
ce3b896151 | ||
|
|
2791cedcba | ||
|
|
583bdf4fc6 | ||
|
|
0ce1b15b99 | ||
|
|
1b19111387 | ||
|
|
44e97d4197 | ||
|
|
6095c996e1 | ||
|
|
2077e9cef2 | ||
|
|
b814f6b555 | ||
|
|
7d6561fee4 | ||
|
|
2890a8c922 | ||
|
|
a050895e4e | ||
|
|
e526d4e653 | ||
|
|
db6cbcadfe |
No files matched your search
@@ -13,9 +13,9 @@ jobs:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
- name: Install Nix
|
||||
uses: cachix/install-nix-action@v24
|
||||
uses: cachix/install-nix-action@v25
|
||||
- name: Update flake.lock
|
||||
uses: DeterminateSystems/update-flake-lock@v20
|
||||
uses: DeterminateSystems/update-flake-lock@v21
|
||||
with:
|
||||
pr-labels: |
|
||||
merge-queue
|
||||
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Nix community projects
|
||||
Copyright (c) 2018, 2019, 2022–2024 Nix community projects
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -57,17 +57,18 @@ To access sample configurations for commonly-used disk layouts, refer to the
|
||||
|
||||
A simple disko configuration may look like this:
|
||||
|
||||
```
|
||||
```nix
|
||||
{
|
||||
disko.devices = {
|
||||
disk = {
|
||||
vdb = {
|
||||
my-disk = {
|
||||
device = "/dev/sda";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "500M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
@@ -92,11 +93,11 @@ A simple disko configuration may look like this:
|
||||
```
|
||||
|
||||
If you'd saved this configuration in /tmp/disko-config.nix, and wanted to create
|
||||
a disk named /dev/nvme0n1, you would run the following command to partition,
|
||||
a disk named /dev/sda, you would run the following command to partition,
|
||||
format and mount the disk.
|
||||
|
||||
```
|
||||
$ sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disko-config.nix
|
||||
```console
|
||||
sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disko-config.nix
|
||||
```
|
||||
|
||||
## Related Tools
|
||||
|
||||
@@ -3,6 +3,6 @@ set -efux -o pipefail
|
||||
# dependencies: bash jq util-linux lvm2 mdadm zfs
|
||||
disk=$(realpath "$1")
|
||||
|
||||
lsblk -a -f >/dev/stderr
|
||||
lsblk -a -f >&2
|
||||
lsblk --output-all --json | jq -r --arg disk_to_clear "$disk" -f "$(dirname "$0")/disk-deactivate.jq" | bash -x
|
||||
lsblk -a -f >/dev/stderr
|
||||
lsblk -a -f >&2
|
||||
@@ -24,8 +24,12 @@ def remove:
|
||||
;
|
||||
|
||||
def deactivate:
|
||||
if .type == "disk" then
|
||||
if .type == "disk" or .type == "loop" then
|
||||
[
|
||||
# If this disk is a member of raid, stop that raid
|
||||
"md_dev=$(lsblk \(.path) -l -p -o type,name | awk 'match($1,\"raid.*\") {print $2}')",
|
||||
"if [[ -n \"${md_dev}\" ]]; then umount \"$md_dev\"; mdadm --stop \"$md_dev\"; fi",
|
||||
# Remove all file-systems and other magic strings
|
||||
"wipefs --all -f \(.path)",
|
||||
# Remove the MBR bootstrap code
|
||||
"dd if=/dev/zero of=\(.path) bs=440 count=1"
|
||||
@@ -50,7 +54,7 @@ def deactivate:
|
||||
"mdadm --stop \(.name)"
|
||||
]
|
||||
else
|
||||
[]
|
||||
["echo Warning: unknown type '\(.type)'. Consider handling this in https://github.com/nix-community/disko/blob/master/disk-deactivate/disk-deactivate.jq"]
|
||||
end
|
||||
;
|
||||
|
||||
|
||||
Executable
+199
@@ -0,0 +1,199 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
showUsage() {
|
||||
cat <<EOF
|
||||
Usage: $0 [OPTIONS]
|
||||
--mode MODE Specify the mode of operation. Valid modes are: format, mount.
|
||||
-f, --flake FLAKE_URI#ATTR Use the specified flake to install the NixOS configuration.
|
||||
--disk NAME DEVICE Map the specified disk name to the specified device path.
|
||||
--dry-run Print the commands that would be run, but do not run them.
|
||||
--show-trace Show the stack trace on error.
|
||||
-h, --help Show this help message.
|
||||
--extra-files SOURCE DEST Copy the specified file or directory from the host into the NixOS configuration.
|
||||
--option NAME VALUE Pass the specified option to Nix.
|
||||
--write-efi-boot-entries Write EFI boot entries to the NVRAM of the system for the installed system.
|
||||
Specify this option if you plan to boot from this disk on the current machine,
|
||||
but not if you plan to move the disk to another machine.
|
||||
EOF
|
||||
}
|
||||
|
||||
serialiaseArrayToNix() {
|
||||
local -n array=$1
|
||||
nixExpr="{ "
|
||||
# Iterate over the associative array to populate the Nix attrset string
|
||||
for key in "${!array[@]}"; do
|
||||
value=${array[$key]}
|
||||
nixExpr+="\"${key//\"/\\\"}\" = \"${value//\"/\\\"}\"; "
|
||||
done
|
||||
nixExpr+="}"
|
||||
|
||||
echo "$nixExpr"
|
||||
}
|
||||
|
||||
readonly libexec_dir="${0%/*}"
|
||||
|
||||
nix_args=(
|
||||
--extra-experimental-features 'nix-command flakes'
|
||||
"--option" "no-write-lock-file" "true"
|
||||
)
|
||||
dry_run=
|
||||
diskoAttr=diskoScript
|
||||
writeEfiBootEntries=false
|
||||
declare -A diskMappings
|
||||
declare -A extraFiles
|
||||
|
||||
parseArgs() {
|
||||
[[ $# -eq 0 ]] && {
|
||||
showUsage
|
||||
exit 1
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-d | --debug)
|
||||
set -x
|
||||
;;
|
||||
-f | --flake)
|
||||
flake=$2
|
||||
shift
|
||||
;;
|
||||
-h | --help)
|
||||
showUsage
|
||||
exit 0
|
||||
;;
|
||||
--dry-run)
|
||||
dry_run=y
|
||||
;;
|
||||
--show-trace)
|
||||
nix_args+=("$1")
|
||||
;;
|
||||
--write-efi-boot-entries)
|
||||
writeEfiBootEntries=true
|
||||
;;
|
||||
--mode)
|
||||
if [[ $# -lt 2 ]]; then
|
||||
echo "Option $1 requires an argument" >&2
|
||||
exit 1
|
||||
fi
|
||||
case $2 in
|
||||
format)
|
||||
diskoAttr=diskoScript
|
||||
;;
|
||||
mount)
|
||||
diskoAttr=mountScript
|
||||
;;
|
||||
*)
|
||||
echo "Invalid mode: $2" >&2
|
||||
echo "Valid modes are: format, mount" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
mode=$2
|
||||
shift
|
||||
;;
|
||||
--extra-files)
|
||||
if [[ $# -lt 3 ]]; then
|
||||
echo "Option $1 requires two arguments: source, destination" >&2
|
||||
exit 1
|
||||
fi
|
||||
extraFiles[$2]=$3
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
--option)
|
||||
if [[ $# -lt 3 ]]; then
|
||||
echo "Option $1 requires an argument" >&2
|
||||
exit 1
|
||||
fi
|
||||
nix_args+=(--option "$2" "$3")
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
--disk)
|
||||
if [[ $# -lt 3 ]]; then
|
||||
echo "Option $1 requires two arguments: disk_name, device_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
# shellcheck disable=SC2034
|
||||
diskMappings[$2]=$3
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1" >&2
|
||||
showUsage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
}
|
||||
|
||||
cleanupMountPoint() {
|
||||
mountPoint=$1
|
||||
if mountpoint -q "${mountPoint}"; then
|
||||
umount -R "${mountPoint}"
|
||||
fi
|
||||
rmdir "${mountPoint}"
|
||||
}
|
||||
|
||||
main() {
|
||||
parseArgs "$@"
|
||||
|
||||
if [[ -z ${flake-} ]]; then
|
||||
echo "Please specify the flake-uri with --flake to use for installation." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
|
||||
flake="${BASH_REMATCH[1]}"
|
||||
flakeAttr="${BASH_REMATCH[2]}"
|
||||
fi
|
||||
if [[ -z ${flakeAttr-} ]]; then
|
||||
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." >&2
|
||||
echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mountPoint=$(mktemp -d)
|
||||
chmod 755 "${mountPoint}" # bchachefs wants 755
|
||||
escapeMountPoint=$(printf '%q' "$mountPoint")
|
||||
# shellcheck disable=SC2064
|
||||
trap "cleanupMountPoint ${escapeMountPoint}" EXIT
|
||||
|
||||
outputs=$(nix-build "${libexec_dir}"/install-cli.nix \
|
||||
"${nix_args[@]}" \
|
||||
--no-out-link \
|
||||
--impure \
|
||||
--argstr flake "$flake" \
|
||||
--argstr flakeAttr "$flakeAttr" \
|
||||
--argstr rootMountPoint "$mountPoint" \
|
||||
--arg writeEfiBootEntries "$writeEfiBootEntries" \
|
||||
--arg diskMappings "$(serialiaseArrayToNix diskMappings)" \
|
||||
-A installToplevel \
|
||||
-A "$diskoAttr")
|
||||
|
||||
IFS=$'\n' mapfile -t artifacts <<<"$outputs"
|
||||
nixos_system=${artifacts[0]}
|
||||
disko_script=${artifacts[1]}
|
||||
|
||||
if [[ -n ${dry_run-} ]]; then
|
||||
echo "Would run: $disko_script"
|
||||
echo "Would run: nixos-install --system '$nixos_system' --root '$mountPoint'"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
"$disko_script"
|
||||
|
||||
for source in "${!extraFiles[@]}"; do
|
||||
destination=${extraFiles[$source]}
|
||||
mkdir -p "$mountPoint/$(dirname "$destination")"
|
||||
cp -ar "$source" "$mountPoint/$destination"
|
||||
done
|
||||
|
||||
nixos-install --no-root-password --system "$nixos_system" --root "$mountPoint"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -0,0 +1,26 @@
|
||||
{ stdenvNoCC, makeWrapper, lib }:
|
||||
|
||||
stdenvNoCC.mkDerivation {
|
||||
name = "disko-install";
|
||||
src = ./.;
|
||||
nativeBuildInputs = [
|
||||
makeWrapper
|
||||
];
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin $out/share/disko
|
||||
cp -r install-cli.nix $out/share/disko
|
||||
sed \
|
||||
-e "s|libexec_dir=\".*\"|libexec_dir=\"$out/share/disko\"|" \
|
||||
-e "s|#!/usr/bin/env.*|#!/usr/bin/env bash|" \
|
||||
disko-install > $out/bin/disko-install
|
||||
chmod 755 $out/bin/disko-install
|
||||
wrapProgram $out/bin/disko-install
|
||||
'';
|
||||
meta = with lib; {
|
||||
description = "Disko and nixos-install in one command";
|
||||
homepage = "https://github.com/nix-community/disko";
|
||||
license = licenses.mit;
|
||||
maintainers = with maintainers; [ lassulus ];
|
||||
platforms = platforms.linux;
|
||||
};
|
||||
}
|
||||
+3
-3
@@ -46,7 +46,7 @@ If you use nix flakes support:
|
||||
First add it to [niv](https://github.com/nmattia/niv):
|
||||
|
||||
```console
|
||||
$ niv add nix-community/disko
|
||||
niv add nix-community/disko
|
||||
```
|
||||
|
||||
Then add the following to your configuration.nix in the `imports` list:
|
||||
@@ -64,8 +64,8 @@ Then add the following to your configuration.nix in the `imports` list:
|
||||
As root run:
|
||||
|
||||
```console
|
||||
$ nix-channel --add https://github.com/nix-community/disko/archive/master.tar.gz disko
|
||||
$ nix-channel --update
|
||||
nix-channel --add https://github.com/nix-community/disko/archive/master.tar.gz disko
|
||||
nix-channel --update
|
||||
```
|
||||
|
||||
Then add the following to your configuration.nix in the `imports` list:
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
- [Quickstart](./quickstart.md)
|
||||
- [System Requirements](./requirements.md)
|
||||
- [How to Guide](./HowTo.md)
|
||||
- [Disko-Install](./disko-install.md)
|
||||
- [Support Matrix](./supportmatrix.md)
|
||||
- [Reference](./reference.md)
|
||||
- [Upgrade Guide](./upgrade-guide.md)
|
||||
@@ -0,0 +1,127 @@
|
||||
# disko-install
|
||||
|
||||
**disko-install** enhances the normal `nixos-install` with disko's partitioning feature.
|
||||
It can be started from the NixOS installer but it can also be used to create bootable USB-Sticks from your normal workstation.
|
||||
Furthermore `disko-install` has a mount mode that will only mount but not destroy existing partitions.
|
||||
The mount mode can be used to mount and repair existing NixOS installations.
|
||||
This document provides a comprehensive guide on how to use **disko-install**, including examples for typical usage scenarios.
|
||||
|
||||
## Requirements
|
||||
|
||||
- a Linux system with Nix installed.
|
||||
- a target disk or partition for the NixOS installation.
|
||||
- a Nix flake that defines your desired NixOS configuration.
|
||||
|
||||
## Usage
|
||||
|
||||
### Fresh Installation
|
||||
|
||||
For a fresh installation, where **disko-install** will handle partitioning and setting up the disk, use the following syntax:
|
||||
|
||||
```console
|
||||
sudo nix run 'github:nix-community/disko#disko-install' -- --flake <flake-url>#<flake-attr> --disk <disk-name> <disk-device>
|
||||
```
|
||||
|
||||
Example:
|
||||
|
||||
First run `nixos-generate-config --root /tmp/config --no-filesystems` and
|
||||
edit `configuration.nix` to your liking.
|
||||
|
||||
Than add the following `flake.nix` inside `/tmp/config/etc/nixos`.
|
||||
In this example we assume a system that has been booted with EFI:
|
||||
|
||||
```nix
|
||||
{
|
||||
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
||||
inputs.disko.url = "github:nix-community/disko";
|
||||
inputs.disko.inputs.nixpkgs.follows = "nixpkgs";
|
||||
|
||||
outputs = { self, disko, nixpkgs }: {
|
||||
nixosConfigurations.mymachine = nixpkgs.legacyPackages.x86_64-linux.nixos [
|
||||
./configuration.nix
|
||||
disko.nixosModules.disko
|
||||
{
|
||||
disko.devices = {
|
||||
disk = {
|
||||
main = {
|
||||
# When using disko-install, we will overwrite this value from the commandline
|
||||
device = "/dev/disk/by-id/some-disk-id";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
MBR = {
|
||||
type = "EF02"; # for grub MBR
|
||||
size = "1M";
|
||||
};
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "500M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = "/boot";
|
||||
};
|
||||
};
|
||||
root = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "ext4";
|
||||
mountpoint = "/";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Identify the device name that you want to install NixOS to:
|
||||
|
||||
```console
|
||||
$ lsblk
|
||||
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
|
||||
sda 8:0 1 14.9G 0 disk
|
||||
└─sda1 8:1 1 14.9G 0 part
|
||||
zd0 230:0 0 10G 0 disk
|
||||
├─zd0p1 230:1 0 500M 0 part
|
||||
└─zd0p2 230:2 0 9.5G 0 part /mnt
|
||||
nvme0n1 259:0 0 1.8T 0 disk
|
||||
├─nvme0n1p1 259:1 0 1G 0 part /boot
|
||||
├─nvme0n1p2 259:2 0 16M 0 part
|
||||
├─nvme0n1p3 259:3 0 250G 0 part
|
||||
└─nvme0n1p4 259:4 0 1.6T 0 part
|
||||
```
|
||||
|
||||
In our example, we want to install to a USB-stick (/dev/sda):
|
||||
|
||||
```console
|
||||
$ sudo nix run 'github:nix-community/disko#disko-install' -- --flake '/tmp/config/etc/nixos#mymachine' --disk main /dev/sda
|
||||
```
|
||||
|
||||
Afterwards you can test your USB-stick by either selecting during the boot
|
||||
or attaching it to a qemu-vm:
|
||||
|
||||
```
|
||||
$ sudo qemu-kvm -enable-kvm -hda /dev/sda
|
||||
```
|
||||
|
||||
### Persisting boot entries to EFI vars flash
|
||||
|
||||
**disko-install** is designed for NixOS installations on portable storage or disks that may be transferred between computers.
|
||||
As such, it does not modify the host's NVRAM by default.
|
||||
To ensure your NixOS installation boots seamlessly on new hardware or to prioritize it in your current machine's boot order,
|
||||
use the --write-efi-boot-entries option:
|
||||
|
||||
```console
|
||||
$ sudo nix run 'github:nix-community/disko#disko-install' -- --write-efi-boot-entries --flake '/tmp/config/etc/nixos#mymachine' --disk main /dev/sda
|
||||
```
|
||||
|
||||
This command installs NixOS with **disko-install** and sets the newly installed system as the default boot option,
|
||||
without affecting the flexibility to boot from other devices if needed.
|
||||
+26
-27
@@ -47,8 +47,8 @@ the NixOS manual. Boot the machine from this USB drive.
|
||||
|
||||
Identify the name of your system disk by using the `lsblk` command as follows:
|
||||
|
||||
```
|
||||
$ lsblk
|
||||
```console
|
||||
lsblk
|
||||
```
|
||||
|
||||
The output from this command will look something like this:
|
||||
@@ -73,9 +73,9 @@ from the url you noted above, using the `-o` option to save the file as
|
||||
disko-config.nix. Your commands would look like this if you had chosen the
|
||||
hybrid layout:
|
||||
|
||||
```
|
||||
```console
|
||||
cd /tmp
|
||||
$ curl https://raw.githubusercontent.com/nix-community/disko/master/example/hybrid.nix -o /tmp/disko-config.nix
|
||||
curl https://raw.githubusercontent.com/nix-community/disko/master/example/hybrid.nix -o /tmp/disko-config.nix
|
||||
```
|
||||
|
||||
### Step 5: Adjust the device in the disk configuration
|
||||
@@ -84,20 +84,20 @@ Inside the disko-config.nix the device needs to point to the correct disk name.
|
||||
|
||||
Open the configuration in your favorite editor i.e.:
|
||||
|
||||
```
|
||||
```console
|
||||
nano /tmp/disko-config.nix
|
||||
```
|
||||
|
||||
Replace `<disk-name>` with the name of your disk obtained in Step 1.
|
||||
|
||||
```
|
||||
...
|
||||
main = {
|
||||
type = "disk";
|
||||
device = "<disk-name>";
|
||||
content = {
|
||||
type = "gpt";
|
||||
...
|
||||
```nix
|
||||
# ...
|
||||
main = {
|
||||
type = "disk";
|
||||
device = "<disk-name>";
|
||||
content = {
|
||||
type = "gpt";
|
||||
# ...
|
||||
```
|
||||
|
||||
### Step 6: Run disko to partition, format and mount your disks
|
||||
@@ -106,16 +106,15 @@ The following step will partition and format your disk, and mount it to `/mnt`.
|
||||
|
||||
**Please note: This will erase any existing data on your disk.**
|
||||
|
||||
```console
|
||||
sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disko-config.nix
|
||||
```
|
||||
$ sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disko-config.nix
|
||||
```
|
||||
|
||||
|
||||
After the command has run, your file system should have been formatted and
|
||||
mounted. You can verify this by running the following command:
|
||||
|
||||
```
|
||||
$ mount | grep /mnt
|
||||
```console
|
||||
mount | grep /mnt
|
||||
```
|
||||
|
||||
The output should look like this if your disk name is `nvme0n1`.
|
||||
@@ -141,16 +140,16 @@ command to generate an initial `configuration.nix`. You will be supplying the
|
||||
file system configuration details from `disko-config.nix`. Your CLI command to
|
||||
generate the configuration will be:
|
||||
|
||||
```
|
||||
$ nixos-generate-config --no-filesystems --root /mnt
|
||||
```console
|
||||
nixos-generate-config --no-filesystems --root /mnt
|
||||
```
|
||||
|
||||
This will create the file `configuration.nix` in `/mnt/etc/nixos`.
|
||||
|
||||
b) Move the `disko` configuration to /etc/nixos
|
||||
|
||||
```
|
||||
$ mv /tmp/disko-config.nix /mnt/etc/nixos
|
||||
```console
|
||||
mv /tmp/disko-config.nix /mnt/etc/nixos
|
||||
```
|
||||
|
||||
c) You can now edit `configuration.nix` as per your requirements. This is
|
||||
@@ -168,7 +167,7 @@ module and `disko-config.nix` to the imports section. This section will already
|
||||
include the file `./hardware-configuration.nix`, and you can add the new entries
|
||||
just below this. This section will now include:
|
||||
|
||||
```
|
||||
```nix
|
||||
imports =
|
||||
[ # Include the results of the hardware scan.
|
||||
./hardware-configuration.nix
|
||||
@@ -186,7 +185,7 @@ existing lines that configure `systemd-boot`. The entries will look like this:
|
||||
**Note:** Its not necessary to set `boot.loader.grub.device` here, since Disko will
|
||||
take care of that automatically.
|
||||
|
||||
```
|
||||
```nix
|
||||
# ...
|
||||
#boot.loader.systemd-boot.enable = true;
|
||||
#boot.loader.efi.canTouchEfiVariables = true;
|
||||
@@ -198,7 +197,7 @@ take care of that automatically.
|
||||
|
||||
f) Finish the installation and reboot your machine,
|
||||
|
||||
```
|
||||
$ nixos-install
|
||||
$ reboot
|
||||
```console
|
||||
nixos-install
|
||||
reboot
|
||||
```
|
||||
+3
-3
@@ -53,13 +53,13 @@ generate disk images:
|
||||
|
||||
1. **Build the disko image script:** Replace `mySystem` in the command below with your
|
||||
specific system configuration name:
|
||||
```bash
|
||||
```console
|
||||
nix build .#nixosConfigurations.mySystem.config.system.build.diskoImagesScript
|
||||
```
|
||||
2. **Execute the result file:** Execute the generated result file. Running
|
||||
`./result --help` will output the available options:
|
||||
|
||||
```
|
||||
```console
|
||||
./result --help
|
||||
Usage: $script [options]
|
||||
|
||||
@@ -85,7 +85,7 @@ generate disk images:
|
||||
|
||||
- For virtual drive use, define the image size in your Disko configuration:
|
||||
|
||||
```bash
|
||||
```console
|
||||
disko.devices.disk.<drive>.imageSize = "32G"; # Set your preferred size
|
||||
```
|
||||
|
||||
|
||||
+2
-2
@@ -35,7 +35,7 @@
|
||||
settings.keyFile = "/tmp/secret.key";
|
||||
additionalKeyFiles = [ "/tmp/additionalSecret.key" ];
|
||||
extraFormatArgs = [
|
||||
"--iter-time 1" # unsecure but fast for tests
|
||||
"--iter-time 1" # insecure but fast for tests
|
||||
];
|
||||
content = {
|
||||
type = "lvm_pv";
|
||||
@@ -64,7 +64,7 @@
|
||||
keyFileOffset = 2;
|
||||
};
|
||||
extraFormatArgs = [
|
||||
"--iter-time 1" # unsecure but fast for tests
|
||||
"--iter-time 1" # insecure but fast for tests
|
||||
];
|
||||
content = {
|
||||
type = "lvm_pv";
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
disko.devices = {
|
||||
disk = {
|
||||
main = {
|
||||
type = "disk";
|
||||
device = "/dev/sdb";
|
||||
content = {
|
||||
type = "gpt";
|
||||
efiGptPartitionFirst = false;
|
||||
partitions = {
|
||||
TOW-BOOT-FI = {
|
||||
priority = 1;
|
||||
type = "EF00";
|
||||
size = "32M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = null;
|
||||
};
|
||||
hybrid = {
|
||||
mbrPartitionType = "0x0c";
|
||||
mbrBootableFlag = false;
|
||||
};
|
||||
};
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "512M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = "/boot";
|
||||
};
|
||||
};
|
||||
root = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "ext4";
|
||||
mountpoint = "/";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
disko.devices = {
|
||||
disk = {
|
||||
vdb = {
|
||||
device = "/dev/disk/by-id/some-disk-id";
|
||||
name = "this-is-some-super-long-name-to-test-what-happens-when-the-name-is-too-long";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "500M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = "/boot";
|
||||
};
|
||||
};
|
||||
root = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "ext4";
|
||||
mountpoint = "/";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -59,6 +59,22 @@
|
||||
};
|
||||
};
|
||||
};
|
||||
a = {
|
||||
type = "disk";
|
||||
device = "/dev/sda";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
zfs = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "zfs";
|
||||
pool = "storage2";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
zpool = {
|
||||
storage = {
|
||||
@@ -73,6 +89,20 @@
|
||||
};
|
||||
};
|
||||
};
|
||||
storage2 = {
|
||||
type = "zpool";
|
||||
mountpoint = "/storage2";
|
||||
rootFsOptions = {
|
||||
canmount = "off";
|
||||
};
|
||||
|
||||
datasets = {
|
||||
dataset = {
|
||||
type = "zfs_fs";
|
||||
mountpoint = "/storage2/dataset";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Generated
+3
-3
@@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1702539185,
|
||||
"narHash": "sha256-KnIRG5NMdLIpEkZTnN5zovNYc0hhXjAgv6pfd5Z4c7U=",
|
||||
"lastModified": 1709386671,
|
||||
"narHash": "sha256-VPqfBnIJ+cfa78pd4Y5Cr6sOWVW8GYHRVucxJGmRf8Q=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "aa9d4729cbc99dabacb50e3994dcefb3ea0f7447",
|
||||
"rev": "fa9a51752f1b5de583ad5213eb621be071806663",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
in
|
||||
{
|
||||
disko = pkgs.callPackage ./package.nix { };
|
||||
disko-install = pkgs.callPackage ./disko-install.nix { };
|
||||
default = self.packages.${system}.disko;
|
||||
} // pkgs.lib.optionalAttrs (!pkgs.buildPlatform.isRiscV64) {
|
||||
disko-doc = pkgs.callPackage ./doc.nix { };
|
||||
@@ -43,6 +44,11 @@
|
||||
makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix");
|
||||
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
|
||||
});
|
||||
|
||||
disko-install = pkgs.callPackage ./tests/disko-install {
|
||||
inherit self;
|
||||
};
|
||||
|
||||
shellcheck = pkgs.runCommand "shellcheck" { nativeBuildInputs = [ pkgs.shellcheck ]; } ''
|
||||
cd ${./.}
|
||||
shellcheck disk-deactivate/disk-deactivate disko
|
||||
@@ -50,8 +56,20 @@
|
||||
'';
|
||||
in
|
||||
# FIXME: aarch64-linux seems to hang on boot
|
||||
nixpkgs.lib.optionalAttrs pkgs.hostPlatform.isx86_64 nixosTests //
|
||||
pkgs.lib.optionalAttrs (!pkgs.buildPlatform.isRiscV64 && !pkgs.hostPlatform.isx86_32) { inherit shellcheck; });
|
||||
nixpkgs.lib.optionalAttrs pkgs.hostPlatform.isx86_64 (nixosTests // { inherit disko-install; }) //
|
||||
pkgs.lib.optionalAttrs (!pkgs.buildPlatform.isRiscV64 && !pkgs.hostPlatform.isx86_32) {
|
||||
inherit shellcheck;
|
||||
inherit (self.packages.${system}) disko-doc;
|
||||
});
|
||||
|
||||
nixosConfigurations.testmachine = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
modules = [
|
||||
./tests/disko-install/configuration.nix
|
||||
./example/hybrid.nix
|
||||
./module.nix
|
||||
];
|
||||
};
|
||||
formatter = forAllSystems (system:
|
||||
let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
{ flake
|
||||
, flakeAttr
|
||||
, diskMappings
|
||||
, writeEfiBootEntries ? false
|
||||
, rootMountPoint ? "/mnt"
|
||||
}:
|
||||
let
|
||||
originalSystem = (builtins.getFlake "${flake}").nixosConfigurations."${flakeAttr}";
|
||||
diskoSystem =
|
||||
let
|
||||
lib = originalSystem.lib;
|
||||
|
||||
modifiedDisks = builtins.mapAttrs
|
||||
(name: value: let
|
||||
dev = if diskMappings ? ${name} then
|
||||
diskMappings.${name}
|
||||
else
|
||||
throw "No device passed for disk '${name}'. Pass `--disk ${name} /dev/name` via commandline";
|
||||
in value // {
|
||||
device = dev;
|
||||
content = value.content // { device = dev; };
|
||||
})
|
||||
originalSystem.config.disko.devices.disk;
|
||||
|
||||
cleanedDisks = lib.filterAttrsRecursive (n: _: !lib.hasPrefix "_" n) modifiedDisks;
|
||||
in
|
||||
originalSystem.extendModules {
|
||||
modules = [{
|
||||
disko.rootMountPoint = rootMountPoint;
|
||||
disko.devices.disk = lib.mkVMOverride cleanedDisks;
|
||||
}];
|
||||
};
|
||||
installSystem = originalSystem.extendModules {
|
||||
modules = [({ lib, ... }: {
|
||||
boot.loader.efi.canTouchEfiVariables = lib.mkVMOverride writeEfiBootEntries;
|
||||
boot.loader.grub.devices = lib.mkVMOverride diskoSystem.config.boot.loader.grub.devices;
|
||||
})];
|
||||
};
|
||||
in
|
||||
{
|
||||
installToplevel = installSystem.config.system.build.toplevel;
|
||||
inherit (diskoSystem.config.system.build) formatScript mountScript diskoScript;
|
||||
}
|
||||
+3
-1
@@ -84,9 +84,11 @@ let
|
||||
"${dev}${toString index}" # /dev/md/raid1 style
|
||||
else if match "/dev/mapper/.+" dev != null then
|
||||
"${dev}${toString index}" # /dev/mapper/vg-lv1 style
|
||||
else if match "/dev/loop[[:digit:]]+" dev != null
|
||||
then "${dev}p${toString index}" # /dev/mapper/vg-lv1 style
|
||||
else
|
||||
abort ''
|
||||
${dev} seems not to be a supported disk format. Please add this to disko in https://github.com/nix-community/disko/blob/master/types/default.nix
|
||||
${dev} seems not to be a supported disk format. Please add this to disko in https://github.com/nix-community/disko/blob/master/lib/default.nix
|
||||
'';
|
||||
|
||||
/* get the index an item in a list
|
||||
|
||||
@@ -10,7 +10,8 @@ let
|
||||
vmTools = pkgs.vmTools.override {
|
||||
rootModules = ["9p" "9pnet_virtio" "virtio_pci" "virtio_blk"] ++ nixosConfig.config.disko.extraRootModules;
|
||||
kernel = pkgs.aggregateModules
|
||||
(with nixosConfig.config.boot.kernelPackages; [ kernel ]);
|
||||
(with nixosConfig.config.boot.kernelPackages; [ kernel ]
|
||||
++ lib.optional (lib.elem "zfs" nixosConfig.config.disko.extraRootModules) zfs);
|
||||
};
|
||||
cleanedConfig = diskoLib.testLib.prepareDiskoConfig nixosConfig.config diskoLib.testLib.devices;
|
||||
systemToInstall = nixosConfig.extendModules {
|
||||
@@ -62,8 +63,9 @@ let
|
||||
'';
|
||||
installer = ''
|
||||
${systemToInstall.config.system.build.nixos-install}/bin/nixos-install --system ${systemToInstall.config.system.build.toplevel} --keep-going --no-channel-copy -v --no-root-password --option binary-caches ""
|
||||
umount -Rv ${systemToInstall.config.disko.rootMountPoint}
|
||||
'';
|
||||
QEMU_OPTS = lib.concatMapStringsSep " " (disk: "-drive file=${disk.name}.raw,if=virtio,cache=unsafe,werror=report") (lib.attrValues nixosConfig.config.disko.devices.disk);
|
||||
QEMU_OPTS = lib.concatMapStringsSep " " (disk: "-drive file=${disk.name}.raw,if=virtio,cache=unsafe,werror=report,format=raw") (lib.attrValues nixosConfig.config.disko.devices.disk);
|
||||
in
|
||||
{
|
||||
pure = vmTools.runInLinuxVM (pkgs.runCommand name
|
||||
|
||||
+23
-6
@@ -114,7 +114,7 @@ let
|
||||
};
|
||||
|
||||
installedTopLevel = ((if extendModules != null then extendModules else installed-system-eval.extendModules) {
|
||||
modules = [{
|
||||
modules = [({config, ...}: {
|
||||
imports = [
|
||||
extraSystemConfig
|
||||
({ modulesPath, ... }: {
|
||||
@@ -141,8 +141,20 @@ let
|
||||
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms
|
||||
|
||||
# grub will install to these devices, we need to force those or we are offset by 1
|
||||
boot.loader.grub.devices = lib.mkForce testConfigInstall.boot.loader.grub.devices;
|
||||
}];
|
||||
# we use mkOveride 70, so that users can override this with mkForce in case they are testing grub mirrored boots
|
||||
boot.loader.grub.devices = lib.mkOverride 70 testConfigInstall.boot.loader.grub.devices;
|
||||
|
||||
assertions = [
|
||||
{
|
||||
assertion = builtins.length config.boot.loader.grub.mirroredBoots > 1 -> config.boot.loader.grub.devices == [];
|
||||
message = ''
|
||||
When using `--vm-test` in combination with `mirroredBoots`,
|
||||
it is necessary to configure `boot.loader.grub.devices` as an empty list by setting `boot.loader.grub.devices = lib.mkForce [];`.
|
||||
This adjustment is crucial because the `--vm-test` mechanism automatically overrides the grub boot devices as part of the virtual machine test.
|
||||
'';
|
||||
}
|
||||
];
|
||||
})];
|
||||
}).config.system.build.toplevel;
|
||||
|
||||
in
|
||||
@@ -211,10 +223,15 @@ let
|
||||
disk_flags += f' -drive file={oldmachine.state_dir}/empty{i}.qcow2,id=drive{i + 1},if=none,index={i + 1},werror=report'
|
||||
disk_flags += f' -device virtio-blk-pci,drive=drive{i + 1}'
|
||||
return disk_flags
|
||||
def create_test_machine(oldmachine=None, args={}): # taken from <nixpkgs/nixos/tests/installer.nix>
|
||||
def create_test_machine(oldmachine, args={}): # taken from <nixpkgs/nixos/tests/installer.nix>
|
||||
startCommand = "${pkgs.qemu_test}/bin/qemu-kvm"
|
||||
startCommand += " -cpu max -m 1024 -virtfs local,path=/nix/store,security_model=none,mount_tag=nix-store"
|
||||
startCommand += disks(oldmachine, ${toString num-disks})
|
||||
${lib.optionalString efi ''
|
||||
startCommand +=" -drive if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware} -drive if=pflash,format=raw,unit=1,readonly=on,file=${pkgs.OVMF.variables}"
|
||||
''}
|
||||
machine = create_machine({
|
||||
"qemuFlags": "-cpu max -m 1024 -virtfs local,path=/nix/store,security_model=none,mount_tag=nix-store" + disks(oldmachine, ${toString num-disks}),
|
||||
${lib.optionalString efi ''"bios": "${pkgs.OVMF.fd}/FV/OVMF.fd",''}
|
||||
"startCommand": startCommand,
|
||||
} | args)
|
||||
driver.machines.append(machine)
|
||||
return machine
|
||||
|
||||
+73
-5
@@ -1,6 +1,7 @@
|
||||
{ config, options, lib, diskoLib, parent, device, ... }:
|
||||
let
|
||||
sortedPartitions = lib.sort (x: y: x.priority < y.priority) (lib.attrValues config.partitions);
|
||||
sortedHybridPartitions = lib.filter (p: p.hybrid != null) sortedPartitions;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
@@ -18,9 +19,19 @@ in
|
||||
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }@partition: {
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.strMatching "[A-Fa-f0-9]{4}";
|
||||
type =
|
||||
let
|
||||
hexPattern = len: "[A-Fa-f0-9]{${toString len}}";
|
||||
in
|
||||
lib.types.either
|
||||
(lib.types.strMatching (hexPattern 4))
|
||||
(lib.types.strMatching (lib.concatMapStringsSep "-" hexPattern [ 8 4 4 4 12 ]));
|
||||
default = "8300";
|
||||
description = "Filesystem type to use, run sgdisk -L to see what is available";
|
||||
description = ''
|
||||
Filesystem type to use.
|
||||
This can either be an sgdisk-specific short code (run sgdisk -L to see what is available),
|
||||
or a fully specified GUID (see https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs).
|
||||
'';
|
||||
};
|
||||
device = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
@@ -44,7 +55,15 @@ in
|
||||
};
|
||||
label = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "${config._parent.type}-${config._parent.name}-${partition.config.name}";
|
||||
default = let
|
||||
# 72 bytes is the maximum length of a GPT partition name
|
||||
# the labels seem to be in UTF-16, so 2 bytes per character
|
||||
limit = 36;
|
||||
label = "${config._parent.type}-${config._parent.name}-${partition.config.name}";
|
||||
in if (lib.stringLength label) > limit then
|
||||
builtins.substring 0 limit (builtins.hashString "sha256" label)
|
||||
else
|
||||
label;
|
||||
};
|
||||
size = lib.mkOption {
|
||||
type = lib.types.either (lib.types.enum [ "100%" ]) (lib.types.strMatching "[0-9]+[KMGTP]?");
|
||||
@@ -70,15 +89,51 @@ in
|
||||
'';
|
||||
};
|
||||
content = diskoLib.partitionType { parent = config; device = partition.config.device; };
|
||||
hybrid = lib.mkOption {
|
||||
type = lib.types.nullOr (lib.types.submodule ({name, ...} @ hp: {
|
||||
options = {
|
||||
mbrPartitionType = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "MBR type code";
|
||||
};
|
||||
mbrBootableFlag = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Set the bootable flag (aka the active flag) on any or all of your hybridized partitions";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = ''
|
||||
${lib.optionalString (hp.config.mbrPartitionType != null) ''
|
||||
sfdisk --label-nested dos --part-type ${parent.device} ${(toString partition.config._index)} ${hp.config.mbrPartitionType}
|
||||
udevadm trigger --subsystem-match=block
|
||||
udevadm settle
|
||||
''}
|
||||
${lib.optionalString hp.config.mbrBootableFlag ''
|
||||
sfdisk --label-nested dos --activate ${parent.device} ${(toString partition.config._index)}
|
||||
''}
|
||||
'';
|
||||
};
|
||||
};
|
||||
}));
|
||||
default = null;
|
||||
description = "Entry to add to the Hybrid MBR table";
|
||||
};
|
||||
_index = lib.mkOption {
|
||||
internal = true;
|
||||
default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0;
|
||||
};
|
||||
};
|
||||
}));
|
||||
default = {};
|
||||
default = { };
|
||||
description = "Attrs of partitions to add to the partition table";
|
||||
};
|
||||
efiGptPartitionFirst = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "Place EFI GPT (0xEE) partition first in MBR (good for GRUB)";
|
||||
};
|
||||
_parent = lib.mkOption {
|
||||
internal = true;
|
||||
default = parent;
|
||||
@@ -100,7 +155,6 @@ in
|
||||
default = ''
|
||||
${lib.concatStrings (map (partition: ''
|
||||
sgdisk \
|
||||
--set-alignment=2048 \
|
||||
--align-end \
|
||||
--new=${toString partition._index}:${partition.start}:${partition.end} \
|
||||
--change-name=${toString partition._index}:${partition.label} \
|
||||
@@ -113,6 +167,20 @@ in
|
||||
${lib.optionalString (partition.content != null) partition.content._create}
|
||||
'') sortedPartitions)}
|
||||
|
||||
${
|
||||
lib.optionalString (sortedHybridPartitions != [])
|
||||
("sgdisk -h "
|
||||
+ (lib.concatStringsSep ":" (map (p: (toString p._index)) sortedHybridPartitions))
|
||||
+ (
|
||||
lib.optionalString (!config.efiGptPartitionFirst) ":EE "
|
||||
)
|
||||
+ parent.device)
|
||||
}
|
||||
${lib.concatMapStrings (p:
|
||||
p.hybrid._create
|
||||
)
|
||||
sortedHybridPartitions}
|
||||
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
|
||||
+15
-10
@@ -15,10 +15,17 @@ let
|
||||
+ "Use passwordFile instead if you want to use interactive login or settings.keyFile if you want to use key file login")
|
||||
config.keyFile
|
||||
else null;
|
||||
keyFileArgs = ''\
|
||||
keyFileArgs = ''
|
||||
${lib.optionalString (keyFile != null) "--key-file ${keyFile}"} \
|
||||
${lib.optionalString (lib.hasAttr "keyFileSize" config.settings) "--keyfile-size ${builtins.toString config.settings.keyFileSize}"} \
|
||||
${lib.optionalString (lib.hasAttr "keyFileOffset" config.settings) "--keyfile-offset ${builtins.toString config.settings.keyFileOffset}"}
|
||||
${lib.optionalString (lib.hasAttr "keyFileOffset" config.settings) "--keyfile-offset ${builtins.toString config.settings.keyFileOffset}"} \
|
||||
'';
|
||||
cryptsetupOpen = ''
|
||||
cryptsetup open ${config.device} ${config.name} \
|
||||
${lib.optionalString (config.settings.allowDiscards or false) "--allow-discards"} \
|
||||
${lib.optionalString (config.settings.bypassWorkqueues or false) "--perf-no_read_workqueue --perf-no_write_workqueue"} \
|
||||
${toString config.extraOpenArgs} \
|
||||
${keyFileArgs} \
|
||||
'';
|
||||
in
|
||||
{
|
||||
@@ -120,12 +127,11 @@ in
|
||||
done
|
||||
set -x
|
||||
''}
|
||||
cryptsetup -q luksFormat ${config.device} ${toString config.extraFormatArgs} \
|
||||
${keyFileArgs}
|
||||
cryptsetup open ${config.device} ${config.name} \
|
||||
${toString config.extraOpenArgs} \
|
||||
${keyFileArgs}
|
||||
${toString (lib.lists.forEach config.additionalKeyFiles (x: "cryptsetup luksAddKey ${config.device} ${x} ${keyFileArgs}"))}
|
||||
cryptsetup -q luksFormat ${config.device} ${toString config.extraFormatArgs} ${keyFileArgs}
|
||||
${cryptsetupOpen} --persistent
|
||||
${toString (lib.forEach config.additionalKeyFiles (keyFile: ''
|
||||
cryptsetup luksAddKey ${config.device} ${keyFile} ${keyFileArgs}
|
||||
''))}
|
||||
${lib.optionalString (config.content != null) config.content._create}
|
||||
'';
|
||||
};
|
||||
@@ -145,8 +151,7 @@ in
|
||||
export password
|
||||
set -x
|
||||
''}
|
||||
cryptsetup open ${config.device} ${config.name} \
|
||||
${keyFileArgs}
|
||||
${cryptsetupOpen}
|
||||
fi
|
||||
${lib.optionalString (config.content != null) contentMount.dev or ""}
|
||||
'';
|
||||
|
||||
@@ -19,6 +19,14 @@
|
||||
default = name;
|
||||
description = "Name of the logical volume";
|
||||
};
|
||||
priority = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
default = if (lib.hasInfix "100%" lv.config.size) then 9001 else 1000;
|
||||
defaultText = lib.literalExpression ''
|
||||
if (lib.hasInfix "100%" lv.config.size) then 9001 else 1000
|
||||
'';
|
||||
description = "Priority of the logical volume, smaller values are created first";
|
||||
};
|
||||
size = lib.mkOption {
|
||||
type = lib.types.str; # TODO lvm size type
|
||||
description = "Size of the logical volume";
|
||||
@@ -56,7 +64,7 @@
|
||||
inherit config options;
|
||||
default =
|
||||
let
|
||||
sortedLvs = lib.sort (a: _: !lib.hasInfix "100%" a.size) (lib.attrValues config.lvs);
|
||||
sortedLvs = lib.sort (a: b: a.priority < b.priority) (lib.attrValues config.lvs);
|
||||
in
|
||||
''
|
||||
readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name})
|
||||
|
||||
+7
-1
@@ -1,6 +1,12 @@
|
||||
{ config, options, lib, diskoLib, parent, device, ... }:
|
||||
{
|
||||
options = {
|
||||
options = lib.warn ''
|
||||
The legacy table is outdated and should not be used. We recommend using the gpt type instead.
|
||||
Please note that certain features, such as the test framework, may not function properly with the legacy table type.
|
||||
If you encounter errors similar to:
|
||||
"error: The option `disko.devices.disk.disk1.content.partitions."[definition 1-entry 1]".content._config` is read-only, but it's set multiple times,"
|
||||
this is likely due to the use of the legacy table type.
|
||||
'' {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "table" ];
|
||||
internal = true;
|
||||
|
||||
+22
-7
@@ -6,17 +6,27 @@
|
||||
default = config._module.args.name;
|
||||
description = "Name of the dataset";
|
||||
};
|
||||
|
||||
_name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "${config._parent.name}/${config.name}";
|
||||
internal = true;
|
||||
description = "Fully quantified name for dataset";
|
||||
};
|
||||
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "zfs_fs" ];
|
||||
default = "zfs_fs";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
|
||||
options = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
description = "Options to set for the dataset";
|
||||
};
|
||||
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
@@ -33,6 +43,7 @@
|
||||
internal = true;
|
||||
default = parent;
|
||||
};
|
||||
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
@@ -40,6 +51,7 @@
|
||||
default = _dev: { };
|
||||
description = "Metadata";
|
||||
};
|
||||
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
# -u prevents mounting newly created datasets, which is
|
||||
@@ -47,17 +59,18 @@
|
||||
# since (create order != mount order)
|
||||
# -p creates parents automatically
|
||||
default = ''
|
||||
zfs create -up ${config._parent.name}/${config.name} \
|
||||
zfs create -up ${config._name} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)}
|
||||
'';
|
||||
};
|
||||
} // { readOnly = false; };
|
||||
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default =
|
||||
lib.optionalAttrs (config.options.mountpoint or "" != "none") {
|
||||
lib.optionalAttrs (config.options.mountpoint or "" != "none" && config.options.canmount or "" != "off") {
|
||||
fs.${config.mountpoint} = ''
|
||||
if ! findmnt ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
|
||||
mount ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" \
|
||||
if ! findmnt ${config._name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
|
||||
mount ${config._name} "${rootMountPoint}${config.mountpoint}" \
|
||||
-o X-mount.mkdir \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
|
||||
@@ -66,19 +79,21 @@
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default =
|
||||
lib.optional (config.options.mountpoint or "" != "none") {
|
||||
lib.optional (config.options.mountpoint or "" != "none" && config.options.canmount or "" != "off") {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = "${config._parent.name}/${config.name}";
|
||||
device = "${config._name}";
|
||||
fsType = "zfs";
|
||||
options = config.mountOptions ++ lib.optional ((config.options.mountpoint or "") != "legacy") "zfsutil";
|
||||
};
|
||||
};
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
|
||||
+14
-23
@@ -67,9 +67,9 @@
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
|
||||
"''${zfs_devices[@]}"
|
||||
${lib.optionalString ((config.rootFsOptions.mountpoint or "") != "none") ''
|
||||
if [[ $(zfs get -H mounted ${config.name} | cut -f3) == "yes" ]]; then
|
||||
zfs unmount ${config.name}
|
||||
''}
|
||||
fi
|
||||
${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)}
|
||||
'';
|
||||
};
|
||||
@@ -85,32 +85,13 @@
|
||||
zpool import -l -R ${rootMountPoint} '${config.name}'
|
||||
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)}
|
||||
'';
|
||||
fs = (datasetMounts.fs or { }) // lib.optionalAttrs (config.mountpoint != null) {
|
||||
${config.mountpoint} = ''
|
||||
if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
|
||||
mount ${config.name} "${rootMountPoint}${config.mountpoint}" \
|
||||
${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
-o X-mount.mkdir \
|
||||
-t zfs
|
||||
fi
|
||||
'';
|
||||
};
|
||||
fs = datasetMounts.fs or { };
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = [
|
||||
(map (dataset: dataset._config) (lib.attrValues config.datasets))
|
||||
(lib.optional (config.mountpoint != null) {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = config.name;
|
||||
fsType = "zfs";
|
||||
options = config.mountOptions ++ lib.optional ((config.options.mountpoint or "") != "legacy") "zfsutil";
|
||||
};
|
||||
})
|
||||
];
|
||||
default = map (dataset: dataset._config) (lib.attrValues config.datasets);
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
@@ -121,4 +102,14 @@
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
datasets."__root" = {
|
||||
_name = config.name;
|
||||
_create = "";
|
||||
type = "zfs_fs";
|
||||
mountpoint = config.mountpoint;
|
||||
options = config.rootFsOptions;
|
||||
};
|
||||
};
|
||||
}
|
||||
+5
-5
@@ -11,11 +11,11 @@ in
|
||||
{
|
||||
options.disko = {
|
||||
extraRootModules = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str ;
|
||||
type = lib.types.listOf lib.types.str;
|
||||
description = ''
|
||||
extra modules to pass to the vmTools.runCommand invocation in the make-disk-image.nix builder
|
||||
'';
|
||||
default = [];
|
||||
default = [ ];
|
||||
};
|
||||
memSize = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
@@ -34,7 +34,7 @@ in
|
||||
description = ''
|
||||
list of extra packages to make available in the make-disk-image.nix VM builder, an example might be f2fs-tools
|
||||
'';
|
||||
default = [];
|
||||
default = [ ];
|
||||
};
|
||||
rootMountPoint = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
@@ -90,8 +90,8 @@ in
|
||||
system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // {
|
||||
|
||||
# we keep these old outputs for compatibility
|
||||
disko = builtins.trace "the .disko output is deprecated, please use .diskoScript instead" (cfg.devices._scripts pkgs).diskoScript;
|
||||
diskoNoDeps = builtins.trace "the .diskoNoDeps output is deprecated, please use .diskoScriptNoDeps instead" (cfg.devices._scripts pkgs).diskoScriptNoDeps;
|
||||
disko = builtins.trace "the .disko output is deprecated, please use .diskoScript instead" (cfg.devices._scripts { inherit pkgs; }).diskoScript;
|
||||
diskoNoDeps = builtins.trace "the .diskoNoDeps output is deprecated, please use .diskoScriptNoDeps instead" (cfg.devices._scripts { inherit pkgs; }).diskoScriptNoDeps;
|
||||
|
||||
diskoImages = diskoLib.makeDiskImages {
|
||||
nixosConfig = args;
|
||||
|
||||
@@ -12,15 +12,5 @@ diskoLib.testLib.makeDiskoTest {
|
||||
# so that the installer boots with a bcachefs enabled kernel
|
||||
extraInstallerConfig = {
|
||||
boot.supportedFilesystems = [ "bcachefs" ];
|
||||
# disable zfs so we can support latest kernel
|
||||
nixpkgs.overlays = [
|
||||
(_final: super: {
|
||||
zfs = super.zfs.overrideAttrs (_: {
|
||||
meta.platforms = [ ];
|
||||
});
|
||||
})
|
||||
];
|
||||
boot.kernelPackages = pkgs.lib.mkForce (pkgs.recurseIntoAttrs pkgs.linuxKernel.packages.linux_testing);
|
||||
};
|
||||
extraSystemConfig.boot.kernelPackages = pkgs.lib.mkForce (pkgs.recurseIntoAttrs pkgs.linuxKernel.packages.linux_testing);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{ lib, pkgs, modulesPath, ... }: {
|
||||
imports = [
|
||||
(modulesPath + "/testing/test-instrumentation.nix")
|
||||
(modulesPath + "/profiles/qemu-guest.nix")
|
||||
(modulesPath + "/profiles/minimal.nix")
|
||||
];
|
||||
|
||||
networking.hostName = "disko-machine";
|
||||
|
||||
# do not try to fetch stuff from the internet
|
||||
nix.settings = {
|
||||
substituters = lib.mkForce [ ];
|
||||
hashed-mirrors = null;
|
||||
connect-timeout = 3;
|
||||
flake-registry = pkgs.writeText "flake-registry" ''{"flakes":[],"version":2}'';
|
||||
experimental-features = [ "nix-command" "flakes" ];
|
||||
};
|
||||
services.openssh.enable = true;
|
||||
boot.kernelParams = [ "console=tty0" ] ++
|
||||
(lib.optional (pkgs.stdenv.hostPlatform.isAarch) "ttyAMA0,115200") ++
|
||||
(lib.optional (pkgs.stdenv.hostPlatform.isRiscV64) "ttySIF0,115200") ++
|
||||
[ "console=ttyS0,115200" ];
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
{ pkgs ? import <nixpkgs> { }, self }:
|
||||
let
|
||||
disko-install = pkgs.callPackage ../../disko-install.nix { };
|
||||
toplevel = self.nixosConfigurations.testmachine.config.system.build.toplevel;
|
||||
|
||||
dependencies = [
|
||||
pkgs.stdenv.drvPath
|
||||
toplevel
|
||||
self.nixosConfigurations.testmachine.config.system.build.diskoScript
|
||||
] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs);
|
||||
|
||||
closureInfo = pkgs.closureInfo { rootPaths = dependencies; };
|
||||
in
|
||||
pkgs.nixosTest {
|
||||
name = "disko-test";
|
||||
nodes.machine = {
|
||||
virtualisation.emptyDiskImages = [ 4096 ];
|
||||
virtualisation.memorySize = 3000;
|
||||
environment.etc."install-closure".source = "${closureInfo}/store-paths";
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
def create_test_machine(oldmachine, args={}): # taken from <nixpkgs/nixos/tests/installer.nix>
|
||||
startCommand = "${pkgs.qemu_test}/bin/qemu-kvm"
|
||||
startCommand += " -cpu max -m 1024 -virtfs local,path=/nix/store,security_model=none,mount_tag=nix-store"
|
||||
startCommand += f' -drive file={oldmachine.state_dir}/empty0.qcow2,id=drive1,if=none,index=1,werror=report'
|
||||
startCommand += ' -device virtio-blk-pci,drive=drive1'
|
||||
machine = create_machine({
|
||||
"startCommand": startCommand,
|
||||
} | args)
|
||||
driver.machines.append(machine)
|
||||
return machine
|
||||
machine.succeed("lsblk >&2")
|
||||
|
||||
print(machine.succeed("tty"))
|
||||
machine.succeed("umask 066; echo > /tmp/age.key")
|
||||
permission = machine.succeed("stat -c %a /tmp/age.key").strip()
|
||||
assert permission == "600", f"expected permission 600 on /tmp/age.key, got {permission}"
|
||||
|
||||
machine.succeed("${disko-install}/bin/disko-install --disk main /dev/vdb --extra-files /tmp/age.key /var/lib/secrets/age.key --flake ${../..}#testmachine")
|
||||
# test idempotency
|
||||
machine.succeed("${disko-install}/bin/disko-install --mode mount --disk main /dev/vdb --flake ${../..}#testmachine")
|
||||
machine.shutdown()
|
||||
|
||||
new_machine = create_test_machine(oldmachine=machine, args={ "name": "after_install" })
|
||||
new_machine.start()
|
||||
name = new_machine.succeed("hostname").strip()
|
||||
assert name == "disko-machine", f"expected hostname 'disko-machine', got {name}"
|
||||
permission = new_machine.succeed("stat -c %a /var/lib/secrets/age.key").strip()
|
||||
assert permission == "600", f"expected permission 600 on /var/lib/secrets/age.key, got {permission}"
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{ pkgs ? import <nixpkgs> { }
|
||||
, diskoLib ? pkgs.callPackage ../lib { }
|
||||
}:
|
||||
diskoLib.testLib.makeDiskoTest {
|
||||
inherit pkgs;
|
||||
name = "hybrid-mbr";
|
||||
disko-config = ../example/hybrid-mbr.nix;
|
||||
extraTestScript = ''
|
||||
machine.succeed("mountpoint /");
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{ pkgs ? import <nixpkgs> { }
|
||||
, diskoLib ? pkgs.callPackage ../lib { }
|
||||
}:
|
||||
diskoLib.testLib.makeDiskoTest {
|
||||
inherit pkgs;
|
||||
name = "long-device-name";
|
||||
disko-config = ../example/long-device-name.nix;
|
||||
extraTestScript = ''
|
||||
machine.succeed("mountpoint /");
|
||||
'';
|
||||
}
|
||||
+23
-1
@@ -14,13 +14,35 @@ diskoLib.testLib.makeDiskoTest {
|
||||
filesystem = machine.execute("stat --file-system --format=%T /mnt/storage")[1].rstrip()
|
||||
print(f"/mnt/storage {filesystem=}")
|
||||
assert filesystem == "zfs", "/mnt/storage is not ZFS"
|
||||
|
||||
machine.fail("mountpoint /mnt/storage2")
|
||||
machine.succeed("mountpoint /mnt/storage2/dataset")
|
||||
|
||||
filesystem = machine.execute("stat --file-system --format=%T /mnt/storage2")[1].rstrip()
|
||||
print(f"/mnt/storage2 {filesystem=}")
|
||||
assert filesystem != "zfs", "/mnt/storage should not be ZFS"
|
||||
|
||||
filesystem = machine.execute("stat --file-system --format=%T /mnt/storage2/dataset")[1].rstrip()
|
||||
print(f"/mnt/storage2/dataset {filesystem=}")
|
||||
assert filesystem == "zfs", "/mnt/storage/dataset is not ZFS"
|
||||
'';
|
||||
extraTestScript = ''
|
||||
machine.succeed("mountpoint /storage")
|
||||
machine.succeed("mountpoint /storage/dataset")
|
||||
|
||||
filesystem = machine.execute("stat --file-system --format=%T /storage")[1].rstrip()
|
||||
print(f"/mnt/storage {filesystem=}")
|
||||
print(f"/storage {filesystem=}")
|
||||
assert filesystem == "zfs", "/storage is not ZFS"
|
||||
|
||||
machine.fail("mountpoint /storage2")
|
||||
machine.succeed("mountpoint /storage2/dataset")
|
||||
|
||||
filesystem = machine.execute("stat --file-system --format=%T /storage2")[1].rstrip()
|
||||
print(f"/storage2 {filesystem=}")
|
||||
assert filesystem != "zfs", "/storage should not be ZFS"
|
||||
|
||||
filesystem = machine.execute("stat --file-system --format=%T /storage2/dataset")[1].rstrip()
|
||||
print(f"/storage2/dataset {filesystem=}")
|
||||
assert filesystem == "zfs", "/storage/dataset is not ZFS"
|
||||
'';
|
||||
}
|
||||
Reference in new issue
Block a user