Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure bootSize in specialArgs #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ on the generated NixOS build.
- hyperv
- proxmox
- qcow
- qcow-efi
- raw-efi
- raw
- vm
Expand All @@ -119,10 +120,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 @@ -158,7 +159,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 @@ -317,14 +319,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