Skip to content

Commit

Permalink
Add disk-size argument
Browse files Browse the repository at this point in the history
Many formats generate an image only large enough for the generated NixOS
config. This quickly runs out of space for NixOS servers that are updated
over time rather than being redeployed with fresh images.

Signed-off-by: James Alseth <james@jalseth.me>
  • Loading branch information
jalseth committed Feb 11, 2024
1 parent 843e2f0 commit 0379113
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 7 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ image was recently merged in nixpkgs):
NIX_PATH=nixpkgs=../nixpkgs nixos-generate -f do
```

## Setting the disk image size

To specify the size of the generated disk image, use the `--disk-size` argument,
specifying the size in megabytes. This is currently supported by the following
formats. If this argument is unspecified it defaults to automatic sizing based
on the generated NixOS build.

- hyperv
- proxmox
- qcow
- raw-efi
- raw
- vm
- vm-nogui
- vmware

Example (20GB disk):
```
nixos-generate -c <your_config.nix> -f <format> --disk-size 20480
```

## Cross Compiling

To cross compile nixos images for other architectures you have to configure
Expand Down
12 changes: 11 additions & 1 deletion formats/hyperv.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{modulesPath, ...}: {
{
modulesPath,
specialArgs,
lib,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/hyperv-image.nix"
];

hyperv.baseImageSize =
if builtins.hasAttr "diskSize" specialArgs == false then "auto"
else if specialArgs.diskSize == "auto" then "auto"
else lib.strings.toIntBase10 specialArgs.diskSize;

formatAttr = "hypervImage";
fileExtension = ".vhdx";
}
9 changes: 8 additions & 1 deletion formats/proxmox.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{modulesPath, ...}: {
{
modulesPath,
specialArgs,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/proxmox-image.nix"
];

proxmox.qemuConf.diskSize = specialArgs.diskSize or "auto";

formatAttr = "VMA";
fileExtension = ".vma.zst";
}
3 changes: 2 additions & 1 deletion formats/qcow.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
lib,
pkgs,
modulesPath,
specialArgs,
...
}: {
# for virtio kernel drivers
Expand All @@ -29,7 +30,7 @@

system.build.qcow = import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = 8192;
diskSize = specialArgs.diskSize or "auto";
format = "qcow2";
partitionTableType = "hybrid";
};
Expand Down
3 changes: 2 additions & 1 deletion formats/raw-efi.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
options,
pkgs,
modulesPath,
specialArgs,
...
}: let
inherit (import ../lib.nix {inherit lib options;}) maybe;
Expand All @@ -24,7 +25,7 @@ in {
system.build.raw = maybe.mkOverride 99 (import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
partitionTableType = "efi";
diskSize = "auto";
diskSize = specialArgs.diskSize or "auto";
format = "raw";
});
}
3 changes: 2 additions & 1 deletion formats/raw.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
lib,
pkgs,
modulesPath,
specialArgs,
...
}: {
fileSystems."/" = {
Expand All @@ -21,7 +22,7 @@

system.build.raw = import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = "auto";
diskSize = specialArgs.diskSize or "auto";
format = "raw";
};

Expand Down
11 changes: 10 additions & 1 deletion formats/vm.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{modulesPath, ...}: {
{
modulesPath,
specialArgs,
lib,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/qemu-vm.nix"
];
virtualisation.diskSize =
if builtins.hasAttr "diskSize" specialArgs == false then null
else if specialArgs.diskSize == "auto" then null
else lib.strings.toIntBase10 specialArgs.diskSize;

formatAttr = "vm";
}
12 changes: 11 additions & 1 deletion formats/vmware.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{modulesPath, ...}: {
{
modulesPath,
specialArgs,
lib,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/vmware-image.nix"
];

vmware.baseImageSize =
if builtins.hasAttr "diskSize" specialArgs == false then "auto"
else if specialArgs.diskSize == "auto" then "auto"
else lib.strings.toIntBase10 specialArgs.diskSize;

formatAttr = "vmwareImage";
fileExtension = ".vmdk";
}
4 changes: 4 additions & 0 deletions nixos-generate
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ while [[ $# -gt 0 ]]; do
cores=$2
shift
;;
--disk-size)
nix_build_args+=("--argstr" "diskSize" "$2")
shift
;;
--option)
nix_build_args+=("$1" "$2" "$3")
shift 2
Expand Down
4 changes: 4 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,
diskSize ? "auto",
formatConfig,
flakeUri ? null,
flakeAttr ? null,
Expand All @@ -20,6 +21,9 @@ in
else
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
specialArgs = {
diskSize = diskSize;
};
modules = [
module
formatConfig
Expand Down

0 comments on commit 0379113

Please sign in to comment.