Compare commits

..
1 Commits
Author SHA1 Message Date
Jörg Thalheim 31787cd754 docs: clarify generated image names 2024-03-11 09:55:04 +01:00
28 changed files with 174 additions and 468 deletions

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install Nix - name: Install Nix
uses: cachix/install-nix-action@v26 uses: cachix/install-nix-action@v25
- name: Update flake.lock - name: Update flake.lock
uses: DeterminateSystems/update-flake-lock@v21 uses: DeterminateSystems/update-flake-lock@v21
with: with:
+2 -2
View File
@@ -92,12 +92,12 @@ A simple disko configuration may look like this:
} }
``` ```
If you'd saved this configuration in /tmp/disk-config.nix, and wanted to create If you'd saved this configuration in /tmp/disko-config.nix, and wanted to create
a disk named /dev/sda, 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. format and mount the disk.
```console ```console
sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disk-config.nix sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disko-config.nix
``` ```
## Related Tools ## Related Tools
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -efux -o pipefail set -efux -o pipefail
# dependencies: bash jq util-linux lvm2 mdadm zfs gnugrep sed # dependencies: bash jq util-linux lvm2 mdadm zfs
disk=$(realpath "$1") disk=$(realpath "$1")
lsblk -a -f >&2 lsblk -a -f >&2
+1 -12
View File
@@ -104,14 +104,6 @@ while [[ $# -gt 0 ]]; do
shift shift
done done
nixBuild() {
if command -v nom-build > /dev/null; then
nom-build "$@"
else
nix-build "$@"
fi
}
if ! { [[ $mode = "format" ]] || [[ $mode = "mount" ]] || [[ $mode = "disko" ]] || [[ $mode = "create" ]] || [[ $mode = "zap_create_mount" ]] ; }; then if ! { [[ $mode = "format" ]] || [[ $mode = "mount" ]] || [[ $mode = "disko" ]] || [[ $mode = "create" ]] || [[ $mode = "zap_create_mount" ]] ; }; then
abort "mode must be either format, mount or disko" abort "mode must be either format, mount or disko"
fi fi
@@ -126,9 +118,6 @@ if [[ -n "${flake+x}" ]]; then
echo "For example, to use the output diskoConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri." echo "For example, to use the output diskoConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri."
exit 1 exit 1
fi fi
if [[ -e "$flake" ]]; then
flake=$(realpath "$flake")
fi
nix_args+=("--arg" "flake" "\"$flake\"") nix_args+=("--arg" "flake" "\"$flake\"")
nix_args+=("--argstr" "flakeAttr" "$flakeAttr") nix_args+=("--argstr" "flakeAttr" "$flakeAttr")
nix_args+=(--extra-experimental-features flakes) nix_args+=(--extra-experimental-features flakes)
@@ -139,7 +128,7 @@ else
fi fi
# The "--impure" is still pure, as the path is within the nix store. # The "--impure" is still pure, as the path is within the nix store.
script=$(nixBuild "${libexec_dir}"/cli.nix \ script=$(nix-build "${libexec_dir}"/cli.nix \
--no-out-link \ --no-out-link \
--impure \ --impure \
--argstr mode "$mode" \ --argstr mode "$mode" \
+2 -23
View File
@@ -6,9 +6,6 @@ showUsage() {
cat <<EOF cat <<EOF
Usage: $0 [OPTIONS] Usage: $0 [OPTIONS]
--mode MODE Specify the mode of operation. Valid modes are: format, mount. --mode MODE Specify the mode of operation. Valid modes are: format, mount.
Format will format the disk before installing.
Mount will mount the disk before installing.
Mount is useful for updating an existing system without losing data.
-f, --flake FLAKE_URI#ATTR Use the specified flake to install the NixOS configuration. -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. --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. --dry-run Print the commands that would be run, but do not run them.
@@ -93,6 +90,7 @@ parseArgs() {
exit 1 exit 1
;; ;;
esac esac
mode=$2
shift shift
;; ;;
--extra-files) --extra-files)
@@ -141,14 +139,6 @@ cleanupMountPoint() {
rmdir "${mountPoint}" rmdir "${mountPoint}"
} }
nixBuild() {
if command -v nom-build > /dev/null; then
nom-build "$@"
else
nix-build "$@"
fi
}
main() { main() {
parseArgs "$@" parseArgs "$@"
@@ -157,21 +147,10 @@ main() {
exit 1 exit 1
fi fi
# check if we are root
if [[ "$EUID" -ne 0 ]]; then
echo "This script must be run as root" >&2
exit 1
fi
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}" flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}" flakeAttr="${BASH_REMATCH[2]}"
fi fi
if [[ -e "$flake" ]]; then
flake=$(realpath "$flake")
fi
if [[ -z ${flakeAttr-} ]]; then 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 "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 echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
@@ -184,7 +163,7 @@ main() {
# shellcheck disable=SC2064 # shellcheck disable=SC2064
trap "cleanupMountPoint ${escapeMountPoint}" EXIT trap "cleanupMountPoint ${escapeMountPoint}" EXIT
outputs=$(nixBuild "${libexec_dir}"/install-cli.nix \ outputs=$(nix-build "${libexec_dir}"/install-cli.nix \
"${nix_args[@]}" \ "${nix_args[@]}" \
--no-out-link \ --no-out-link \
--impure \ --impure \
+2 -25
View File
@@ -53,30 +53,7 @@ Then add the following to your configuration.nix in the `imports` list:
```nix ```nix
{ {
imports = [ "${(import ./nix/sources.nix).disko}/module.nix" ]; imports = [ "${(import ./nix/sources.nix).disko}/modules/disko.nix" ];
}
```
</details>
<details>
<summary>npins</summary>
First add it to [npins](https://github.com/andir/npins):
```console
npins add github nix-community disko
```
Then add the following to your configuration.nix in the `imports` list:
```nix
let
sources = import ./npins;
disko = import sources.disko {};
in
{
imports = [ "${disko}/module.nix" ];
…
} }
``` ```
@@ -95,7 +72,7 @@ Then add the following to your configuration.nix in the `imports` list:
```nix ```nix
{ {
imports = [ <disko/module.nix> ]; imports = [ <disko/modules/disko.nix> ];
} }
``` ```
-86
View File
@@ -125,89 +125,3 @@ $ sudo nix run 'github:nix-community/disko#disko-install' -- --write-efi-boot-en
This command installs NixOS with **disko-install** and sets the newly installed system as the default boot option, 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. without affecting the flexibility to boot from other devices if needed.
### Using disko-install in an offline installer
If you want to **disko-install** from a customer installer without internet, you need to make sure that
next the toplevel of your NixOS closure that you plan to install, it also needs **diskoScript** and
all the flake inputs for evaluation.
#### Example configuration to install
Add this to your flake.nix output:
```nix
{
nixosConfigurations.your-machine = inputs.nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
{
# TODO: add your NixOS configuration here, don't forget your hardware-configuration.nix as well!
boot.loader.systemd-boot.enable = true;
imports = [ self.inputs.disko.nixosModules.disko ];
disko.devices = {
disk = {
vdb = {
device = "/dev/disk/by-id/some-disk-id";
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 = "/";
};
};
};
};
};
};
};
}
];
};
}
```
#### Example for a NixOS installer
```nix
# `self` here is referring to the flake `self`, you may need to pass it using `specialArgs` or define your NixOS installer configuration
# in the flake.nix itself to get direct access to the `self` flake variable.
{ pkgs, self, ... }:
let
dependencies = [
pkgs.stdenv.drvPath
self.nixosConfigurations.your-machine.config.system.build.toplevel
self.nixosConfigurations.your-machine.config.system.build.diskoScript
] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs);
closureInfo = pkgs.closureInfo { rootPaths = dependencies; };
in
# Now add `closureInfo` to your NixOS installer
{
environment.etc."install-closure".source = "${closureInfo}/store-paths";
environment.systemPackages = [
(pkgs.writeShellScriptBin "install-nixos-unattended" ''
set -eux
# Replace "/dev/disk/by-id/some-disk-id" with your actual disk ID
exec ${pkgs.disko}/bin/disko-install --flake "${self}#your-machine" --disk vdb "/dev/disk/by-id/some-disk-id"
'')
];
}
```
Also see the [NixOS test of disko-install](https://github.com/nix-community/disko/blob/master/tests/disko-install/default.nix) that also runs without internet.
+11 -11
View File
@@ -68,24 +68,24 @@ In Step 1, you chose a disk layout configuration from the
and made a note of its URL. and made a note of its URL.
Your configuration needs to be saved on the new machine for example Your configuration needs to be saved on the new machine for example
as /tmp/disk-config.nix. You can do this using the `curl` command to download as /tmp/disko-config.nix. You can do this using the `curl` command to download
from the url you noted above, using the `-o` option to save the file as from the url you noted above, using the `-o` option to save the file as
disk-config.nix. Your commands would look like this if you had chosen the disko-config.nix. Your commands would look like this if you had chosen the
hybrid layout: hybrid layout:
```console ```console
cd /tmp cd /tmp
curl https://raw.githubusercontent.com/nix-community/disko/master/example/hybrid.nix -o /tmp/disk-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 ### Step 5: Adjust the device in the disk configuration
Inside the disk-config.nix the device needs to point to the correct disk name. Inside the disko-config.nix the device needs to point to the correct disk name.
Open the configuration in your favorite editor i.e.: Open the configuration in your favorite editor i.e.:
```console ```console
nano /tmp/disk-config.nix nano /tmp/disko-config.nix
``` ```
Replace `<disk-name>` with the name of your disk obtained in Step 1. Replace `<disk-name>` with the name of your disk obtained in Step 1.
@@ -107,7 +107,7 @@ 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.** **Please note: This will erase any existing data on your disk.**
```console ```console
sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko /tmp/disk-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 After the command has run, your file system should have been formatted and
@@ -131,13 +131,13 @@ the NixOS installation as described in the
[NixOS manual](https://nixos.org/manual/nixos/stable/index.html#sec-installation) - [NixOS manual](https://nixos.org/manual/nixos/stable/index.html#sec-installation) -
see the section headed "**Installing**", Steps 3 onwards. However, you will need see the section headed "**Installing**", Steps 3 onwards. However, you will need
to include the partitioning and formatting configurations that you copied into to include the partitioning and formatting configurations that you copied into
`/tmp/disk-config.nix` in your configuration, rather than allowing NixOS to `/tmp/disko-config.nix` in your configuration, rather than allowing NixOS to
generate information about your file systems. When you are configuring the generate information about your file systems. When you are configuring the
system as per Step 4 of the manual, you should: system as per Step 4 of the manual, you should:
a) Include the `no-filesystems` switch when using the `nixos-generate-config` a) Include the `no-filesystems` switch when using the `nixos-generate-config`
command to generate an initial `configuration.nix`. You will be supplying the command to generate an initial `configuration.nix`. You will be supplying the
file system configuration details from `disk-config.nix`. Your CLI command to file system configuration details from `disko-config.nix`. Your CLI command to
generate the configuration will be: generate the configuration will be:
```console ```console
@@ -149,7 +149,7 @@ This will create the file `configuration.nix` in `/mnt/etc/nixos`.
b) Move the `disko` configuration to /etc/nixos b) Move the `disko` configuration to /etc/nixos
```console ```console
mv /tmp/disk-config.nix /mnt/etc/nixos mv /tmp/disko-config.nix /mnt/etc/nixos
``` ```
c) You can now edit `configuration.nix` as per your requirements. This is c) You can now edit `configuration.nix` as per your requirements. This is
@@ -163,7 +163,7 @@ NixOS configuration in the manual:
[Example: NixOS Configuration](https://nixos.org/manual/nixos/stable/index.html#ex-config). [Example: NixOS Configuration](https://nixos.org/manual/nixos/stable/index.html#ex-config).
d) When editing `configuration.nix`, you will need to add the `disko` NixOS d) When editing `configuration.nix`, you will need to add the `disko` NixOS
module and `disk-config.nix` to the imports section. This section will already 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 include the file `./hardware-configuration.nix`, and you can add the new entries
just below this. This section will now include: just below this. This section will now include:
@@ -172,7 +172,7 @@ imports =
[ # Include the results of the hardware scan. [ # Include the results of the hardware scan.
./hardware-configuration.nix ./hardware-configuration.nix
"${builtins.fetchTarball "https://github.com/nix-community/disko/archive/master.tar.gz"}/module.nix" "${builtins.fetchTarball "https://github.com/nix-community/disko/archive/master.tar.gz"}/module.nix"
./disk-config.nix ./disko-config.nix
]; ];
``` ```
-37
View File
@@ -1,37 +0,0 @@
{
disko.devices = {
disk = {
vdb = {
type = "disk";
device = "/dev/disk/by-diskseq/1";
content = {
type = "gpt";
partitions = {
ESP = {
priority = 1;
name = "ESP";
start = "1M";
end = "128M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
size = "100%";
content = {
type = "btrfs";
extraArgs = [ "-f" ]; # Override existing partition
mountpoint = "/";
mountOptions = [ "compress=zstd" "noatime" ];
};
};
};
};
};
};
};
}
+1 -1
View File
@@ -52,7 +52,7 @@
"com.sun:auto-snapshot" = "false"; "com.sun:auto-snapshot" = "false";
}; };
mountpoint = "/"; mountpoint = "/";
postCreateHook = "zfs list -t snapshot -H -o name | grep -E '^zroot@blank$' || zfs snapshot zroot@blank"; postCreateHook = "zfs snapshot zroot@blank";
datasets = { datasets = {
zfs_fs = { zfs_fs = {
Generated
+3 -3
View File
@@ -2,11 +2,11 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1713687659, "lastModified": 1710097495,
"narHash": "sha256-Yd8KuOBpZ0Slau/NxFhMPJI0gBxeax0vq/FD0rqKwuQ=", "narHash": "sha256-B7Ea7q7hU7SE8wOPJ9oXEBjvB89yl2csaLjf5v/7jr8=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "f2d7a289c5a5ece8521dd082b81ac7e4a57c2c5c", "rev": "d40e866b1f98698d454dad8f592fe7616ff705a4",
"type": "github" "type": "github"
}, },
"original": { "original": {
+31 -48
View File
@@ -1,58 +1,41 @@
{ { flake
flake, , flakeAttr
flakeAttr, , diskMappings
diskMappings, , writeEfiBootEntries ? false
writeEfiBootEntries ? false, , rootMountPoint ? "/mnt"
rootMountPoint ? "/mnt",
}: }:
let let
originalSystem = (builtins.getFlake "${flake}").nixosConfigurations."${flakeAttr}"; originalSystem = (builtins.getFlake "${flake}").nixosConfigurations."${flakeAttr}";
lib = originalSystem.pkgs.lib; diskoSystem =
deviceName =
name:
if diskMappings ? ${name} then
diskMappings.${name}
else
throw "No device passed for disk '${name}'. Pass `--disk ${name} /dev/name` via commandline";
modifiedDisks = builtins.mapAttrs (
name: value:
let let
dev = deviceName name; 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 in
value originalSystem.extendModules {
// { modules = [{
device = dev;
content = value.content // {
device = dev;
};
}
) originalSystem.config.disko.devices.disk;
# filter all nixos module internal attributes
cleanedDisks = lib.filterAttrsRecursive (n: _: !lib.hasPrefix "_" n) modifiedDisks;
diskoSystem = originalSystem.extendModules {
modules = [
{
disko.rootMountPoint = rootMountPoint; disko.rootMountPoint = rootMountPoint;
disko.devices.disk = lib.mkVMOverride cleanedDisks; disko.devices.disk = lib.mkVMOverride cleanedDisks;
} }];
]; };
}; installSystem = originalSystem.extendModules {
modules = [({ lib, ... }: {
installSystem = originalSystem.extendModules { boot.loader.efi.canTouchEfiVariables = lib.mkVMOverride writeEfiBootEntries;
modules = [ boot.loader.grub.devices = lib.mkVMOverride diskoSystem.config.boot.loader.grub.devices;
( })];
{ lib, ... }: };
{
boot.loader.efi.canTouchEfiVariables = lib.mkVMOverride writeEfiBootEntries;
boot.loader.grub.devices = lib.mkVMOverride diskoSystem.config.boot.loader.grub.devices;
}
)
];
};
in in
{ {
installToplevel = installSystem.config.system.build.toplevel; installToplevel = installSystem.config.system.build.toplevel;
+1 -2
View File
@@ -400,7 +400,6 @@ let
lvm2 lvm2
bash bash
jq jq
gnused
])}:$PATH ])}:$PATH
${cfg.config._destroy} ${cfg.config._destroy}
''; '';
@@ -449,7 +448,7 @@ let
# shellcheck disable=SC2043 # shellcheck disable=SC2043
for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do
$BASH ${../disk-deactivate}/disk-deactivate "$dev" ${../disk-deactivate}/disk-deactivate "$dev"
done done
''; '';
}; };
+2 -10
View File
@@ -243,23 +243,15 @@ let
# running direct mode # running direct mode
machine.succeed("${tsp-format}") machine.succeed("${tsp-format}")
machine.succeed("${tsp-mount}") machine.succeed("${tsp-mount}")
machine.succeed("${tsp-mount}") # verify that mount is idempotent machine.succeed("${tsp-mount}") # verify that the command is idempotent
machine.succeed("${tsp-disko}") # verify that we can destroy and recreate machine.succeed("${tsp-disko}") # verify that we can destroy and recreate
machine.succeed("mkdir -p /mnt/home")
machine.succeed("touch /mnt/home/testfile")
machine.succeed("${tsp-format}") # verify that format is idempotent
machine.succeed("test -e /mnt/home/testfile")
''} ''}
${lib.optionalString (testMode == "module") '' ${lib.optionalString (testMode == "module") ''
# running module mode # running module mode
machine.succeed("${nodes.machine.system.build.formatScript}") machine.succeed("${nodes.machine.system.build.formatScript}")
machine.succeed("${nodes.machine.system.build.mountScript}") machine.succeed("${nodes.machine.system.build.mountScript}")
machine.succeed("${nodes.machine.system.build.mountScript}") # verify that mount is idempotent machine.succeed("${nodes.machine.system.build.mountScript}") # verify that the command is idempotent
machine.succeed("${nodes.machine.system.build.diskoScript}") # verify that we can destroy and recreate again machine.succeed("${nodes.machine.system.build.diskoScript}") # verify that we can destroy and recreate again
machine.succeed("mkdir -p /mnt/home")
machine.succeed("touch /mnt/home/testfile")
machine.succeed("${nodes.machine.system.build.formatScript}") # verify that format is idempotent
machine.succeed("test -e /mnt/home/testfile")
''} ''}
${postDisko} ${postDisko}
+21 -34
View File
@@ -31,11 +31,7 @@ let
swapCreate = mountpoint: swap: swapCreate = mountpoint: swap:
lib.concatMapStringsSep lib.concatMapStringsSep
"\n" "\n"
(file: '' (file: ''btrfs filesystem mkswapfile --size ${file.size} "${mountpoint}/${file.path}"'')
if ! test -e "${mountpoint}/${file.path}"; then
btrfs filesystem mkswapfile --size ${file.size} "${mountpoint}/${file.path}"
fi
'')
(lib.attrValues swap); (lib.attrValues swap);
in in
@@ -116,35 +112,26 @@ in
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
# create the filesystem only if the device seems empty mkfs.btrfs ${config.device} ${toString config.extraArgs}
if ! (blkid '${config.device}' -o export | grep -q '^TYPE='); then ${lib.optionalString (config.swap != {}) ''
mkfs.btrfs "${config.device}" ${toString config.extraArgs} (
fi MNTPOINT=$(mktemp -d)
${lib.optionalString (config.swap != {} || config.subvolumes != {}) '' mount ${device} "$MNTPOINT" -o subvol=/
if (blkid "${config.device}" -o export | grep -q '^TYPE=btrfs$'); then trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
${lib.optionalString (config.swap != {}) '' ${swapCreate "$MNTPOINT" config.swap}
( )
MNTPOINT=$(mktemp -d)
mount ${device} "$MNTPOINT" -o subvol=/
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
${swapCreate "$MNTPOINT" config.swap}
)
''}
${lib.concatMapStrings (subvol: ''
(
MNTPOINT=$(mktemp -d)
mount ${config.device} "$MNTPOINT" -o subvol=/
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}"
mkdir -p "$(dirname "$SUBVOL_ABS_PATH")"
if ! btrfs subvolume show "$SUBVOL_ABS_PATH" > /dev/null 2>&1; then
btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs}
fi
${swapCreate "$SUBVOL_ABS_PATH" subvol.swap}
)
'') (lib.attrValues config.subvolumes)}
fi
''} ''}
${lib.concatMapStrings (subvol: ''
(
MNTPOINT=$(mktemp -d)
mount ${config.device} "$MNTPOINT" -o subvol=/
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}"
mkdir -p "$(dirname "$SUBVOL_ABS_PATH")"
btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs}
${swapCreate "$SUBVOL_ABS_PATH" subvol.swap}
)
'') (lib.attrValues config.subvolumes)}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
@@ -219,7 +206,7 @@ in
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: default = pkgs:
[ pkgs.btrfs-progs pkgs.coreutils pkgs.gnugrep ]; [ pkgs.btrfs-progs pkgs.coreutils ];
description = "Packages"; description = "Packages";
}; };
}; };
+4 -7
View File
@@ -44,11 +44,9 @@
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! (blkid '${config.device}' | grep -q 'TYPE='); then mkfs.${config.format} \
mkfs.${config.format} \ ${toString config.extraArgs} \
${toString config.extraArgs} \ ${config.device}
${config.device}
fi
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
@@ -81,12 +79,11 @@
readOnly = true; readOnly = true;
# type = lib.types.functionTo (lib.types.listOf lib.types.package); # type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: default = pkgs:
[ pkgs.util-linux pkgs.gnugrep ] ++ ( [ pkgs.util-linux ] ++ (
# TODO add many more # TODO add many more
if (config.format == "xfs") then [ pkgs.xfsprogs ] if (config.format == "xfs") then [ pkgs.xfsprogs ]
else if (config.format == "btrfs") then [ pkgs.btrfs-progs ] else if (config.format == "btrfs") then [ pkgs.btrfs-progs ]
else if (config.format == "vfat") then [ pkgs.dosfstools ] else if (config.format == "vfat") then [ pkgs.dosfstools ]
else if (config.format == "exfat") then [ pkgs.exfat ]
else if (config.format == "ext2") then [ pkgs.e2fsprogs ] else if (config.format == "ext2") then [ pkgs.e2fsprogs ]
else if (config.format == "ext3") then [ pkgs.e2fsprogs ] else if (config.format == "ext3") then [ pkgs.e2fsprogs ]
else if (config.format == "ext4") then [ pkgs.e2fsprogs ] else if (config.format == "ext4") then [ pkgs.e2fsprogs ]
+5 -22
View File
@@ -74,11 +74,6 @@ in
can be 100% for the whole remaining disk, will be done last in that case. can be 100% for the whole remaining disk, will be done last in that case.
''; '';
}; };
alignment = lib.mkOption {
type = lib.types.int;
default = if (builtins.substring (builtins.stringLength partition.config.start - 1) 1 partition.config.start == "s" || (builtins.substring (builtins.stringLength partition.config.end - 1) 1 partition.config.end == "s" )) then 1 else 0;
description = "Alignment of the partition, if sectors are used as start or end it can be aligned to 1";
};
start = lib.mkOption { start = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "0"; default = "0";
@@ -158,26 +153,18 @@ in
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! blkid "${config.device}" >&2; then
sgdisk --clear ${config.device}
fi
${lib.concatStrings (map (partition: '' ${lib.concatStrings (map (partition: ''
# try to create the partition, if it fails, try to change the type and name sgdisk \
if ! sgdisk \ --align-end \
--align-end ${lib.optionalString (partition.alignment != 0) ''--set-alignment=${builtins.toString partition.alignment}''} \
--new=${toString partition._index}:${partition.start}:${partition.end} \ --new=${toString partition._index}:${partition.start}:${partition.end} \
--change-name=${toString partition._index}:${partition.label} \ --change-name=${toString partition._index}:${partition.label} \
--typecode=${toString partition._index}:${partition.type} \ --typecode=${toString partition._index}:${partition.type} \
${config.device} ${config.device}
then sgdisk \
--change-name=${toString partition._index}:${partition.label} \
--typecode=${toString partition._index}:${partition.type} \
${config.device}
fi
# ensure /dev/disk/by-path/..-partN exists before continuing # ensure /dev/disk/by-path/..-partN exists before continuing
partprobe ${config.device} || : # sometimes partprobe fails, but the partitions are still up2date partprobe ${config.device}
udevadm trigger --subsystem-match=block udevadm trigger --subsystem-match=block
udevadm settle udevadm settle
${lib.optionalString (partition.content != null) partition.content._create}
'') sortedPartitions)} '') sortedPartitions)}
${ ${
@@ -192,12 +179,8 @@ in
${lib.concatMapStrings (p: ${lib.concatMapStrings (p:
p.hybrid._create p.hybrid._create
) )
sortedHybridPartitions sortedHybridPartitions}
}
${lib.concatStrings (map (partition: ''
${lib.optionalString (partition.content != null) partition.content._create}
'') sortedPartitions)}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
+21 -23
View File
@@ -112,28 +112,26 @@ in
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! blkid "${config.device}" >/dev/null || ! (blkid "${config.device}" -o export | grep -q '^TYPE='); then ${lib.optionalString config.askPassword ''
${lib.optionalString config.askPassword '' set +x
set +x askPassword() {
askPassword() { echo "Enter password for ${config.device}: "
echo "Enter password for ${config.device}: " IFS= read -r -s password
IFS= read -r -s password echo "Enter password for ${config.device} again to be safe: "
echo "Enter password for ${config.device} again to be safe: " IFS= read -r -s password_check
IFS= read -r -s password_check export password
export password [ "$password" = "$password_check" ]
[ "$password" = "$password_check" ] }
} until askPassword; do
until askPassword; do echo "Passwords did not match, please try again."
echo "Passwords did not match, please try again." done
done set -x
set -x ''}
''} cryptsetup -q luksFormat ${config.device} ${toString config.extraFormatArgs} ${keyFileArgs}
cryptsetup -q luksFormat ${config.device} ${toString config.extraFormatArgs} ${keyFileArgs} ${cryptsetupOpen} --persistent
${cryptsetupOpen} --persistent ${toString (lib.forEach config.additionalKeyFiles (keyFile: ''
${toString (lib.forEach config.additionalKeyFiles (keyFile: '' cryptsetup luksAddKey ${config.device} ${keyFile} ${keyFileArgs}
cryptsetup luksAddKey ${config.device} ${keyFile} ${keyFileArgs} ''))}
''))}
fi
${lib.optionalString (config.content != null) config.content._create} ${lib.optionalString (config.content != null) config.content._create}
''; '';
}; };
@@ -178,7 +176,7 @@ in
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.gnugrep pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs)); default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs));
description = "Packages"; description = "Packages";
}; };
}; };
+2 -4
View File
@@ -31,9 +31,7 @@
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! (blkid '${config.device}' | grep -q 'TYPE='); then pvcreate ${config.device}
pvcreate ${config.device}
fi
echo "${config.device}" >>"$disko_devices_dir"/lvm_${config.vg} echo "${config.device}" >>"$disko_devices_dir"/lvm_${config.vg}
''; '';
}; };
@@ -51,7 +49,7 @@
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.gnugrep pkgs.lvm2 ]; default = pkgs: [ pkgs.lvm2 ];
description = "Packages"; description = "Packages";
}; };
}; };
+9 -16
View File
@@ -68,23 +68,16 @@
in in
'' ''
readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name}) readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name})
if ! vgdisplay "${config.name}" >/dev/null; then vgcreate ${config.name} \
vgcreate ${config.name} \ "''${lvm_devices[@]}"
"''${lvm_devices[@]}"
fi
${lib.concatMapStrings (lv: ''
if ! lvdisplay '${config.name}/${lv.name}'; then
lvcreate \
--yes \
${if lib.hasInfix "%" lv.size then "-l" else "-L"} ${lv.size} \
-n ${lv.name} \
${lib.optionalString (lv.lvm_type != null) "--type=${lv.lvm_type}"} \
${toString lv.extraArgs} \
${config.name}
fi
'') sortedLvs}
${lib.concatMapStrings (lv: '' ${lib.concatMapStrings (lv: ''
lvcreate \
--yes \
${if lib.hasInfix "%" lv.size then "-l" else "-L"} ${lv.size} \
-n ${lv.name} \
${lib.optionalString (lv.lvm_type != null) "--type=${lv.lvm_type}"} \
${toString lv.extraArgs} \
${config.name}
${lib.optionalString (lv.content != null) lv.content._create} ${lib.optionalString (lv.content != null) lv.content._create}
'') sortedLvs} '') sortedLvs}
''; '';
+13 -15
View File
@@ -34,21 +34,19 @@
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! test -e /dev/md/${config.name}; then readarray -t disk_devices < <(cat "$disko_devices_dir"/raid_${config.name})
readarray -t disk_devices < <(cat "$disko_devices_dir"/raid_${config.name}) echo 'y' | mdadm --create /dev/md/${config.name} \
echo 'y' | mdadm --create /dev/md/${config.name} \ --level=${toString config.level} \
--level=${toString config.level} \ --raid-devices="$(wc -l "$disko_devices_dir"/raid_${config.name} | cut -f 1 -d " ")" \
--raid-devices="$(wc -l "$disko_devices_dir"/raid_${config.name} | cut -f 1 -d " ")" \ --metadata=${config.metadata} \
--metadata=${config.metadata} \ --force \
--force \ --homehost=any \
--homehost=any \ "''${disk_devices[@]}"
"''${disk_devices[@]}" partprobe /dev/md/${config.name}
udevadm trigger --subsystem-match=block udevadm trigger --subsystem-match=block
udevadm settle udevadm settle
# for some reason mdadm devices spawn with an existing partition table, so we need to wipe it # for some reason mdadm devices spawn with an existing partition table, so we need to wipe it
sgdisk --zap-all /dev/md/${config.name} sgdisk --zap-all /dev/md/${config.name}
partprobe /dev/md/${config.name}
fi
${lib.optionalString (config.content != null) config.content._create} ${lib.optionalString (config.content != null) config.content._create}
''; '';
}; };
+3 -5
View File
@@ -40,11 +40,9 @@
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! blkid "${config.device}" -o export | grep -q '^TYPE='; then mkswap \
mkswap \ ${toString config.extraArgs} \
${toString config.extraArgs} \ ${config.device}
${config.device}
fi
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
+21 -25
View File
@@ -88,32 +88,28 @@
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! blkid "${config.device}" >/dev/null; then parted -s ${config.device} -- mklabel ${config.format}
parted -s ${config.device} -- mklabel ${config.format}
${lib.concatStrings (map (partition: ''
${lib.optionalString (config.format == "gpt") ''
parted -s ${config.device} -- mkpart ${partition.name} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
${lib.optionalString (config.format == "msdos") ''
parted -s ${config.device} -- mkpart ${partition.part-type} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
# ensure /dev/disk/by-path/..-partN exists before continuing
partprobe ${config.device}
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString partition.bootable ''
parted -s ${config.device} -- set ${toString partition._index} boot on
''}
${lib.concatMapStringsSep "" (flag: ''
parted -s ${config.device} -- set ${toString partition._index} ${flag} on
'') partition.flags}
# ensure further operations can detect new partitions
partprobe ${config.device}
udevadm trigger --subsystem-match=block
udevadm settle
'') config.partitions)}
fi
${lib.concatStrings (map (partition: '' ${lib.concatStrings (map (partition: ''
${lib.optionalString (config.format == "gpt") ''
parted -s ${config.device} -- mkpart ${partition.name} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
${lib.optionalString (config.format == "msdos") ''
parted -s ${config.device} -- mkpart ${partition.part-type} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
# ensure /dev/disk/by-path/..-partN exists before continuing
partprobe ${config.device}
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString partition.bootable ''
parted -s ${config.device} -- set ${toString partition._index} boot on
''}
${lib.concatMapStringsSep "" (flag: ''
parted -s ${config.device} -- set ${toString partition._index} ${flag} on
'') partition.flags}
# ensure further operations can detect new partitions
partprobe ${config.device}
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString (partition.content != null) partition.content._create} ${lib.optionalString (partition.content != null) partition.content._create}
'') config.partitions)} '') config.partitions)}
''; '';
+2 -4
View File
@@ -59,10 +59,8 @@
# since (create order != mount order) # since (create order != mount order)
# -p creates parents automatically # -p creates parents automatically
default = '' default = ''
if ! zfs get type ${config._name} >/dev/null 2>&1; then zfs create -up ${config._name} \
zfs create -up ${config._name} \ ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)}
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)}
fi
''; '';
} // { readOnly = false; }; } // { readOnly = false; };
+7 -9
View File
@@ -47,15 +47,13 @@
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = '' default = ''
if ! zfs get type ${config._parent.name}/${config.name} >/dev/null 2>&1; then zfs create ${config._parent.name}/${config.name} \
zfs create ${config._parent.name}/${config.name} \ ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \ -V ${config.size}
-V ${config.size} zvol_wait
zvol_wait partprobe /dev/zvol/${config._parent.name}/${config.name}
partprobe /dev/zvol/${config._parent.name}/${config.name} udevadm trigger --subsystem-match=block
udevadm trigger --subsystem-match=block udevadm settle
udevadm settle
fi
${lib.optionalString (config.content != null) config.content._create} ${lib.optionalString (config.content != null) config.content._create}
''; '';
}; };
+8 -30
View File
@@ -62,35 +62,13 @@
inherit config options; inherit config options;
default = '' default = ''
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name}) readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
if zpool list '${config.name}'; then zpool create -f ${config.name} \
echo "not creating zpool ${config.name} as a pool with that name already exists" >&2 -R ${rootMountPoint} ${config.mode} \
else ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
continue=1 ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
for dev in "''${zfs_devices[@]}"; do "''${zfs_devices[@]}"
if ! blkid "$dev" >/dev/null; then if [[ $(zfs get -H mounted ${config.name} | cut -f3) == "yes" ]]; then
# blkid fails, so device seems empty zfs unmount ${config.name}
:
elif (blkid "$dev" -o export | grep '^PTUUID='); then
echo "device $dev already has a partuuid, skipping creating zpool ${config.name}" >&2
continue=0
elif (blkid "$dev" -o export | grep '^TYPE=zfs_member'); then
# zfs_member is a zfs partition, so we try to add the device to the pool
:
elif (blkid "$dev" -o export | grep '^TYPE='); then
echo "device $dev already has a partition, skipping creating zpool ${config.name}" >&2
continue=0
fi
done
if [ $continue -eq 1 ]; then
zpool create -f ${config.name} \
-R ${rootMountPoint} ${config.mode} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
"''${zfs_devices[@]}"
if [[ $(zfs get -H mounted ${config.name} | cut -f3) == "yes" ]]; then
zfs unmount ${config.name}
fi
fi
fi fi
${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)} ${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)}
''; '';
@@ -120,7 +98,7 @@
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.gnugrep pkgs.util-linux ] ++ lib.flatten (map (dataset: dataset._pkgs pkgs) (lib.attrValues config.datasets)); default = pkgs: [ pkgs.util-linux ] ++ lib.flatten (map (dataset: dataset._pkgs pkgs) (lib.attrValues config.datasets));
description = "Packages"; description = "Packages";
}; };
}; };
-11
View File
@@ -1,11 +0,0 @@
{ pkgs ? import <nixpkgs> { }
, diskoLib ? pkgs.callPackage ../lib { }
}:
diskoLib.testLib.makeDiskoTest {
inherit pkgs;
name = "btrfs-only-root-subvolume";
disko-config = ../example/btrfs-only-root-subvolume.nix;
extraTestScript = ''
machine.succeed("btrfs subvolume list /");
'';
}
-1
View File
@@ -8,7 +8,6 @@ diskoLib.makeDiskImages {
({ config, ... }: { ({ config, ... }: {
documentation.enable = false; documentation.enable = false;
system.stateVersion = config.system.nixos.version; system.stateVersion = config.system.nixos.version;
disko.memSize = 2048;
}) })
]; ];
} }