Skip to content

Commit

Permalink
Allow to configure bootSize in specialArgs
Browse files Browse the repository at this point in the history
Update `qcow-efi` to not define module options and use `specialArgs`
as the rest of the formats, in order to improve format backend
consistency. Porting all of them to module options would be cleaner
though.
  • Loading branch information
ereslibre committed Aug 27, 2024
1 parent 0552f78 commit ecbb9a4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 56 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ on the generated NixOS build.
Example (20GB disk):

```bash
nixos-generate -c <your_config.nix> -f <format> --disk-size 20480
nixos-generate -c <your_config.nix> -f <format> --boot-size 512 --disk-size 20480
```

To set the disk size in `flake.nix`, set `diskSize` in the `specialArgs` argument of the `nixosGenerate` function.
To set the disk size in `flake.nix`, set `diskSize` in the `specialArgs` argument of the `nixosGenerate` function. Similarly, you can also set `bootSize` in the `specialArgs` argument in the same manner, as well as use the `--boot-size` argument to specify the size in megabytes for the `/boot` partition.

```nix
{
Expand Down Expand Up @@ -157,7 +157,8 @@ To set the disk size in `flake.nix`, set `diskSize` in the `specialArgs` argumen
system = system;
specialArgs = {
pkgs = pkgs;
diskSize = 20 * 1024;
bootSize = 512; # 500 MB for /boot
diskSize = 20 * 1024; # 20 GB for /
};
modules = [
# Pin nixpkgs to the flake input, so that the packages installed
Expand Down Expand Up @@ -316,14 +317,14 @@ multiple custom formats. `nixosGenerate` will then match against these custom f
# ./configuration.nix
];
format = "vmware";
# optional arguments:
# explicit nixpkgs and lib:
# pkgs = nixpkgs.legacyPackages.x86_64-linux;
# lib = nixpkgs.legacyPackages.x86_64-linux.lib;
# additional arguments to pass to modules:
# specialArgs = { myExtraArg = "foobar"; };
# you can also define your own custom formats
# customFormats = { "myFormat" = <myFormatModule>; ... };
# format = "myFormat";
Expand Down
7 changes: 6 additions & 1 deletion formats/proxmox.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
lib,
modulesPath,
specialArgs,
...
Expand All @@ -7,7 +8,11 @@
"${toString modulesPath}/virtualisation/proxmox-image.nix"
];

proxmox.qemuConf.diskSize = specialArgs.diskSize or "auto";
proxmox.qemuConf = {
diskSize = specialArgs.diskSize or "auto";
} // (lib.optionalAttrs ((builtins.hasAttr "bootSize" specialArgs) && specialArgs.bootSize != null) {
bootSize = "${builtins.toString specialArgs.bootSize}M";
});

formatAttr = "VMA";
fileExtension = ".vma.zst";
Expand Down
85 changes: 38 additions & 47 deletions formats/qcow-efi.nix
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
{ config, lib, pkgs, modulesPath, ... }:
{
config,
lib,
pkgs,
modulesPath,
specialArgs,
...
}: let
consoles = [ "ttyS0" ] ++
(lib.optional (pkgs.stdenv.hostPlatform.isAarch) "ttyAMA0,115200") ++
(lib.optional (pkgs.stdenv.hostPlatform.isRiscV64) "ttySIF0,115200");
in {
# for virtio kernel drivers
imports = [
"${toString modulesPath}/profiles/qemu-guest.nix"
];

options = {
boot = {
consoles = lib.mkOption {
default = [ "ttyS0" ] ++
(lib.optional (pkgs.stdenv.hostPlatform.isAarch) "ttyAMA0,115200") ++
(lib.optional (pkgs.stdenv.hostPlatform.isRiscV64) "ttySIF0,115200");
description = "Kernel console boot flags to pass to boot.kernelParams";
example = [ "ttyS2,115200" ];
};

diskSize = lib.mkOption {
default = "auto";
description = "The disk size in megabytes of the system disk image.";
type = with lib.types; oneOf [ ints.positive (enum [ "auto" ])];
};
};
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
autoResize = true;
fsType = "ext4";
};

config = {
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
autoResize = true;
fsType = "ext4";
};

fileSystems."/boot" = {
device = "/dev/disk/by-label/ESP";
fsType = "vfat";
};

boot.growPartition = true;
boot.kernelParams = map (c: "console=${c}") config.boot.consoles;
boot.loader.grub.device = "nodev";

boot.loader.grub.efiSupport = true;
boot.loader.grub.efiInstallAsRemovable = true;
boot.loader.timeout = 0;

system.build.qcow-efi = import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = config.boot.diskSize;
format = "qcow2";
partitionTableType = "efi";
};

formatAttr = "qcow-efi";
fileExtension = ".qcow2";
fileSystems."/boot" = {
device = "/dev/disk/by-label/ESP";
fsType = "vfat";
};
}

boot.growPartition = true;
boot.kernelParams = map (c: "console=${c}") consoles;
boot.loader.grub.device = "nodev";

boot.loader.grub.efiSupport = true;
boot.loader.grub.efiInstallAsRemovable = true;
boot.loader.timeout = 0;

system.build.qcow-efi = import "${toString modulesPath}/../lib/make-disk-image.nix" ({
inherit lib config pkgs;
diskSize = specialArgs.diskSize or "auto";
format = "qcow2";
partitionTableType = "efi";
} // (lib.optionalAttrs ((builtins.hasAttr "bootSize" specialArgs) && specialArgs.bootSize != null) {
bootSize = "${builtins.toString specialArgs.bootSize}M";
}));

formatAttr = "qcow-efi";
fileExtension = ".qcow2";
}
6 changes: 4 additions & 2 deletions formats/qcow.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
boot.loader.grub.efiInstallAsRemovable = lib.mkIf (pkgs.stdenv.system != "x86_64-linux") (lib.mkDefault true);
boot.loader.timeout = 0;

system.build.qcow = import "${toString modulesPath}/../lib/make-disk-image.nix" {
system.build.qcow = import "${toString modulesPath}/../lib/make-disk-image.nix" ({
inherit lib config pkgs;
diskSize = specialArgs.diskSize or "auto";
format = "qcow2";
partitionTableType = "hybrid";
};
} // (lib.optionalAttrs ((builtins.hasAttr "bootSize" specialArgs) && specialArgs.bootSize != null) {
bootSize = "${builtins.toString specialArgs.bootSize}M";
}));

formatAttr = "qcow";
fileExtension = ".qcow2";
Expand Down
4 changes: 3 additions & 1 deletion formats/raw-efi.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ in {
partitionTableType = "efi";
diskSize = specialArgs.diskSize or "auto";
format = "raw";
});
} // (lib.optionalAttrs ((builtins.hasAttr "bootSize" specialArgs) && specialArgs.bootSize != null) {
bootSize = "${builtins.toString specialArgs.bootSize}M";
}));
}
4 changes: 4 additions & 0 deletions nixos-generate
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ while [[ $# -gt 0 ]]; do
cores=$2
shift
;;
--boot-size)
nix_build_args+=("--argstr" "bootSize" "$2")
shift
;;
--disk-size)
nix_build_args+=("--argstr" "diskSize" "$2")
shift
Expand Down
2 changes: 2 additions & 0 deletions nixos-generate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
nixpkgs ? <nixpkgs>,
configuration ? <nixos-config>,
system ? builtins.currentSystem,
bootSize ? null,
diskSize ? "auto",
formatConfig,
flakeUri ? null,
Expand All @@ -22,6 +23,7 @@ in
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
specialArgs = {
bootSize = bootSize;
diskSize = diskSize;
};
modules = [
Expand Down

0 comments on commit ecbb9a4

Please sign in to comment.