2023-07-25 00:19:20 +02:00
|
|
|
# This module exposes options to build a disk image with a GUID Partition Table
|
|
|
|
# (GPT). It uses systemd-repart to build the image.
|
|
|
|
|
|
|
|
{ config, pkgs, lib, utils, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.image.repart;
|
|
|
|
|
|
|
|
partitionOptions = {
|
|
|
|
options = {
|
|
|
|
storePaths = lib.mkOption {
|
|
|
|
type = with lib.types; listOf path;
|
|
|
|
default = [ ];
|
|
|
|
description = lib.mdDoc "The store paths to include in the partition.";
|
|
|
|
};
|
|
|
|
|
|
|
|
stripNixStorePrefix = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to strip `/nix/store/` from the store paths. This is useful
|
|
|
|
when you want to build a partition that only contains store paths and
|
|
|
|
is mounted under `/nix/store`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
contents = lib.mkOption {
|
|
|
|
type = with lib.types; attrsOf (submodule {
|
|
|
|
options = {
|
|
|
|
source = lib.mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = lib.mdDoc "Path of the source file.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = { };
|
2023-10-31 10:49:56 +01:00
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
"/EFI/BOOT/BOOTX64.EFI".source =
|
|
|
|
"''${pkgs.systemd}/lib/systemd/boot/efi/systemd-bootx64.efi";
|
2023-07-25 00:19:20 +02:00
|
|
|
|
2023-10-31 10:49:56 +01:00
|
|
|
"/loader/entries/nixos.conf".source = systemdBootEntry;
|
|
|
|
}
|
2023-07-25 00:19:20 +02:00
|
|
|
'';
|
|
|
|
description = lib.mdDoc "The contents to end up in the filesystem image.";
|
|
|
|
};
|
|
|
|
|
|
|
|
repartConfig = lib.mkOption {
|
|
|
|
type = with lib.types; attrsOf (oneOf [ str int bool ]);
|
|
|
|
example = {
|
|
|
|
Type = "home";
|
|
|
|
SizeMinBytes = "512M";
|
|
|
|
SizeMaxBytes = "2G";
|
|
|
|
};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Specify the repart options for a partiton as a structural setting.
|
|
|
|
See <https://www.freedesktop.org/software/systemd/man/repart.d.html>
|
|
|
|
for all available options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-03-07 18:24:00 +01:00
|
|
|
|
|
|
|
mkfsOptionsToEnv = opts: lib.mapAttrs' (fsType: options: {
|
|
|
|
name = "SYSTEMD_REPART_MKFS_OPTIONS_${lib.toUpper fsType}";
|
|
|
|
value = builtins.concatStringsSep " " options;
|
|
|
|
}) opts;
|
2023-07-25 00:19:20 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.image.repart = {
|
|
|
|
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2023-12-30 00:15:17 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Name of the image.
|
|
|
|
|
|
|
|
If this option is unset but config.system.image.id is set,
|
|
|
|
config.system.image.id is used as the default value.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
version = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
default = config.system.image.version;
|
|
|
|
defaultText = lib.literalExpression "config.system.image.version";
|
|
|
|
description = lib.mdDoc "Version of the image";
|
|
|
|
};
|
|
|
|
|
|
|
|
imageFileBasename = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
readOnly = true;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Basename of the image filename without any extension (e.g. `image_1`).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
imageFile = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
readOnly = true;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Filename of the image including all extensions (e.g `image_1.raw` or
|
|
|
|
`image_1.raw.zst`).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
compression = {
|
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Image compression");
|
|
|
|
|
|
|
|
algorithm = lib.mkOption {
|
|
|
|
type = lib.types.enum [ "zstd" "xz" ];
|
|
|
|
default = "zstd";
|
|
|
|
description = lib.mdDoc "Compression algorithm";
|
|
|
|
};
|
|
|
|
|
|
|
|
level = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Compression level. The available range depends on the used algorithm.
|
|
|
|
'';
|
|
|
|
};
|
2023-07-25 00:19:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
seed = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
# Generated with `uuidgen`. Random but fixed to improve reproducibility.
|
|
|
|
default = "0867da16-f251-457d-a9e8-c31f9a3c220b";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A UUID to use as a seed. You can set this to `null` to explicitly
|
|
|
|
randomize the partition UUIDs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
split = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Enables generation of split artifacts from partitions. If enabled, for
|
|
|
|
each partition with SplitName= set, a separate output file containing
|
|
|
|
just the contents of that partition is generated.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-01-27 07:26:46 +01:00
|
|
|
sectorSize = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr int;
|
|
|
|
default = 512;
|
|
|
|
example = lib.literalExpression "4096";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The sector size of the disk image produced by systemd-repart. This
|
|
|
|
value must be a power of 2 between 512 and 4096.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 13:32:45 +02:00
|
|
|
package = lib.mkPackageOption pkgs "systemd-repart" {
|
2023-12-18 22:33:04 +01:00
|
|
|
# We use buildPackages so that repart images are built with the build
|
|
|
|
# platform's systemd, allowing for cross-compiled systems to work.
|
|
|
|
default = [ "buildPackages" "systemd" ];
|
|
|
|
example = "pkgs.buildPackages.systemdMinimal.override { withCryptsetup = true; }";
|
2023-08-04 13:32:45 +02:00
|
|
|
};
|
|
|
|
|
2023-07-25 00:19:20 +02:00
|
|
|
partitions = lib.mkOption {
|
|
|
|
type = with lib.types; attrsOf (submodule partitionOptions);
|
|
|
|
default = { };
|
2023-10-31 10:49:56 +01:00
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
"10-esp" = {
|
|
|
|
contents = {
|
|
|
|
"/EFI/BOOT/BOOTX64.EFI".source =
|
|
|
|
"''${pkgs.systemd}/lib/systemd/boot/efi/systemd-bootx64.efi";
|
|
|
|
}
|
|
|
|
repartConfig = {
|
|
|
|
Type = "esp";
|
|
|
|
Format = "fat";
|
|
|
|
};
|
2023-07-25 00:19:20 +02:00
|
|
|
};
|
2023-10-31 10:49:56 +01:00
|
|
|
"20-root" = {
|
|
|
|
storePaths = [ config.system.build.toplevel ];
|
|
|
|
repartConfig = {
|
|
|
|
Type = "root";
|
|
|
|
Format = "ext4";
|
|
|
|
Minimize = "guess";
|
|
|
|
};
|
2023-07-25 00:19:20 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Specify partitions as a set of the names of the partitions with their
|
|
|
|
configuration as the key.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-07 18:24:00 +01:00
|
|
|
mkfsOptions = lib.mkOption {
|
|
|
|
type = with lib.types; attrsOf (listOf str);
|
|
|
|
default = {};
|
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
vfat = [ "-S 512" "-c" ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Specify extra options for created file systems. The specified options
|
|
|
|
are converted to individual environment variables of the format
|
|
|
|
`SYSTEMD_REPART_MKFS_OPTIONS_<FSTYPE>`.
|
|
|
|
|
|
|
|
See [upstream systemd documentation](https://github.com/systemd/systemd/blob/v255/docs/ENVIRONMENT.md?plain=1#L575-L577)
|
|
|
|
for information about the usage of these environment variables.
|
|
|
|
|
|
|
|
The example would produce the following environment variable:
|
|
|
|
```
|
|
|
|
SYSTEMD_REPART_MKFS_OPTIONS_VFAT="-S 512 -c"
|
|
|
|
```
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-19 17:42:22 +01:00
|
|
|
finalPartitions = lib.mkOption {
|
|
|
|
type = lib.types.attrs;
|
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Convenience option to access partitions with added closures.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-07-25 00:19:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
2023-12-30 00:15:17 +01:00
|
|
|
image.repart =
|
|
|
|
let
|
|
|
|
version = config.image.repart.version;
|
|
|
|
versionInfix = if version != null then "_${version}" else "";
|
|
|
|
compressionSuffix = lib.optionalString cfg.compression.enable
|
|
|
|
{
|
|
|
|
"zstd" = ".zst";
|
|
|
|
"xz" = ".xz";
|
|
|
|
}."${cfg.compression.algorithm}";
|
2024-03-19 17:42:22 +01:00
|
|
|
|
|
|
|
makeClosure = paths: pkgs.closureInfo { rootPaths = paths; };
|
|
|
|
|
|
|
|
# Add the closure of the provided Nix store paths to cfg.partitions so
|
|
|
|
# that amend-repart-definitions.py can read it.
|
|
|
|
addClosure = _name: partitionConfig: partitionConfig // (
|
|
|
|
lib.optionalAttrs
|
|
|
|
(partitionConfig.storePaths or [ ] != [ ])
|
|
|
|
{ closure = "${makeClosure partitionConfig.storePaths}/store-paths"; }
|
|
|
|
);
|
2023-12-30 00:15:17 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
name = lib.mkIf (config.system.image.id != null) (lib.mkOptionDefault config.system.image.id);
|
|
|
|
imageFileBasename = cfg.name + versionInfix;
|
|
|
|
imageFile = cfg.imageFileBasename + ".raw" + compressionSuffix;
|
|
|
|
|
|
|
|
compression = {
|
|
|
|
# Generally default to slightly faster than default compression
|
|
|
|
# levels under the assumption that most of the building will be done
|
|
|
|
# for development and release builds will be customized.
|
|
|
|
level = lib.mkOptionDefault {
|
|
|
|
"zstd" = 3;
|
|
|
|
"xz" = 3;
|
|
|
|
}."${cfg.compression.algorithm}";
|
|
|
|
};
|
2024-03-19 17:42:22 +01:00
|
|
|
|
|
|
|
finalPartitions = lib.mapAttrs addClosure cfg.partitions;
|
2023-12-30 00:15:17 +01:00
|
|
|
};
|
|
|
|
|
2023-07-25 00:19:20 +02:00
|
|
|
system.build.image =
|
|
|
|
let
|
|
|
|
fileSystems = lib.filter
|
|
|
|
(f: f != null)
|
|
|
|
(lib.mapAttrsToList (_n: v: v.repartConfig.Format or null) cfg.partitions);
|
|
|
|
|
|
|
|
|
|
|
|
format = pkgs.formats.ini { };
|
|
|
|
|
|
|
|
definitionsDirectory = utils.systemdUtils.lib.definitions
|
|
|
|
"repart.d"
|
|
|
|
format
|
2024-03-19 17:42:22 +01:00
|
|
|
(lib.mapAttrs (_n: v: { Partition = v.repartConfig; }) cfg.finalPartitions);
|
2023-07-25 00:19:20 +02:00
|
|
|
|
2024-03-19 17:42:22 +01:00
|
|
|
partitionsJSON = pkgs.writeText "partitions.json" (builtins.toJSON cfg.finalPartitions);
|
2024-03-07 18:24:00 +01:00
|
|
|
|
|
|
|
mkfsEnv = mkfsOptionsToEnv cfg.mkfsOptions;
|
2023-07-25 00:19:20 +02:00
|
|
|
in
|
2023-12-18 22:33:04 +01:00
|
|
|
pkgs.callPackage ./repart-image.nix {
|
|
|
|
systemd = cfg.package;
|
nixos/repart-image: refactor to use mkDerivation
As a follow-up to https://github.com/NixOS/nixpkgs/pull/294096 this
should further improve the flexibility around building OS images with
systemd-repart:
* Previously the attribute set `compression` needed to be fully
populated, including `algorithm` and `level` because
`compression.enable` was evaluated by bash, after being interpolated
as strings into the `buildCommand`. Now it's sufficient to pass
`compression.enable = false` to the builder, e.g. in `overrideAttrs`,
to disable the compression.
* Using mkDerivation allows for much more customization than the
previously used `runCommand`, making use of phases and pre/post hooks.
This is especially helpful for building multiple images from the same
system configuration, e.g. to build an image `Y` based on a partially
built raw image `X`, by injecting a UKI that depends on `X` into a
defered ESP.
* Before this change it was non-trivial to conduct further manipulations
on the amended repart definitions. Now, the definitions that
systemd-repart uses to build the image can be easily manipulated in
`postPatch` or `preBuild`.
Aside from this, the build is now executed in the build directory, rather
than `$out`. This allows references to relative paths in the build
environment to be used, especially for `--definitions`, which previously
required an absolute path.
2024-03-19 17:04:47 +01:00
|
|
|
inherit (cfg) name version imageFileBasename compression split seed sectorSize;
|
|
|
|
inherit fileSystems definitionsDirectory partitionsJSON mkfsEnv;
|
2023-12-18 22:33:04 +01:00
|
|
|
};
|
2023-07-25 00:19:20 +02:00
|
|
|
|
2024-03-19 17:43:23 +01:00
|
|
|
meta.maintainers = with lib.maintainers; [ nikstur willibutz ];
|
2023-07-25 00:19:20 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
}
|