Skip to content

Commit

Permalink
Merge pull request #292 from jalseth/master
Browse files Browse the repository at this point in the history
Add disk-size argument
  • Loading branch information
Lassulus committed Feb 12, 2024
2 parents 843e2f0 + 4c7b914 commit 3b6bde6
Show file tree
Hide file tree
Showing 10 changed files with 79 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
13 changes: 12 additions & 1 deletion formats/hyperv.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{modulesPath, ...}: {
{
modulesPath,
specialArgs,
lib,
...
}: let
diskSize = specialArgs.diskSize or "auto";
in {
imports = [
"${toString modulesPath}/virtualisation/hyperv-image.nix"
];

hyperv.baseImageSize =
if diskSize == "auto" then "auto"
else lib.strings.toIntBase10 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
13 changes: 12 additions & 1 deletion formats/vm.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
{modulesPath, ...}: {
{
modulesPath,
specialArgs,
lib,
...
}: let
diskSize = specialArgs.diskSize or "auto";
in {
imports = [
"${toString modulesPath}/virtualisation/qemu-vm.nix"
];

virtualisation.diskSize =
if diskSize == "auto" then null
else lib.strings.toIntBase10 diskSize;

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

vmware.baseImageSize =
if diskSize == "auto" then "auto"
else lib.strings.toIntBase10 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 3b6bde6

Please sign in to comment.