Skip to content

Commit

Permalink
nixos-modules/microvm/options: move all user-input options here
Browse files Browse the repository at this point in the history
so they end up in the doc
  • Loading branch information
astro committed Jan 9, 2025
1 parent 70db31a commit 3394c37
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 93 deletions.
81 changes: 30 additions & 51 deletions nixos-modules/microvm/optimization.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,35 @@ let
) config.microvm.shares;

in
{
options.microvm.optimize = {
enable = lib.mkOption {
description = ''
Enables some optimizations by default to closure size and startup time:
- defaults documentation to off
- defaults to using systemd in initrd
- use systemd-networkd
- disables systemd-network-wait-online
- disables NixOS system switching if the host store is not mounted
This takes a few hundred MB off the closure size, including qemu,
allowing for putting MicroVMs inside Docker containers.
'';

type = lib.types.bool;
default = true;
};
};

config = lib.mkIf (cfg.guest.enable && cfg.optimize.enable) {
# The docs are pretty chonky
documentation.enable = lib.mkDefault false;

# Use systemd initrd for startup speed.
# TODO: error mounting /nix/store on crosvm, kvmtool
boot.initrd.systemd.enable = lib.mkDefault (
builtins.elem cfg.hypervisor [
"qemu"
"cloud-hypervisor"
"firecracker"
"stratovirt"
]);

nixpkgs.overlays = [
(final: prev: {
stratovirt = prev.stratovirt.override { gtk3 = null; };
})
];

# networkd is used due to some strange startup time issues with nixos's
# homegrown dhcp implementation
networking.useNetworkd = lib.mkDefault true;
# Due to a bug in systemd-networkd: https://github.com/systemd/systemd/issues/29388
# we cannot use systemd-networkd-wait-online.
systemd.network.wait-online.enable = lib.mkDefault false;

# Exclude switch-to-configuration.pl from toplevel.
system = lib.optionalAttrs (options.system ? switch && !canSwitchViaSsh) {
switch.enable = lib.mkDefault false;
};
lib.mkIf (cfg.guest.enable && cfg.optimize.enable) {
# The docs are pretty chonky
documentation.enable = lib.mkDefault false;

# Use systemd initrd for startup speed.
# TODO: error mounting /nix/store on crosvm, kvmtool
boot.initrd.systemd.enable = lib.mkDefault (
builtins.elem cfg.hypervisor [
"qemu"
"cloud-hypervisor"
"firecracker"
"stratovirt"
]);

nixpkgs.overlays = [
(final: prev: {
stratovirt = prev.stratovirt.override { gtk3 = null; };
})
];

# networkd is used due to some strange startup time issues with nixos's
# homegrown dhcp implementation
networking.useNetworkd = lib.mkDefault true;
# Due to a bug in systemd-networkd: https://github.com/systemd/systemd/issues/29388
# we cannot use systemd-networkd-wait-online.
systemd.network.wait-online.enable = lib.mkDefault false;

# Exclude switch-to-configuration.pl from toplevel.
system = lib.optionalAttrs (options.system ? switch && !canSwitchViaSsh) {
switch.enable = lib.mkDefault false;
};
}
60 changes: 59 additions & 1 deletion nixos-modules/microvm/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ in
'';
};

optimize.enable = lib.mkOption {
description = ''
Enables some optimizations by default to closure size and startup time:
- defaults documentation to off
- defaults to using systemd in initrd
- use systemd-networkd
- disables systemd-network-wait-online
- disables NixOS system switching if the host store is not mounted
This takes a few hundred MB off the closure size, including qemu,
allowing for putting MicroVMs inside Docker containers.
'';

type = lib.types.bool;
default = true;
};

cpu = mkOption {
type = with types; nullOr str;
default = null;
Expand Down Expand Up @@ -519,14 +536,55 @@ in
defaultText = literalExpression ''"config.microvm.runner.''${config.microvm.hypervisor}"'';
};

# TODO: microvm-* as well?
binScripts = mkOption {
description = ''
Script snippets that end up in the runner package's bin/ directory
'';
default = {};
type = with types; attrsOf lines;
};

storeDiskType = mkOption {
type = types.enum [ "squashfs" "erofs" ];
description = ''
Boot disk file system type: squashfs is smaller, erofs is supposed to be faster.
Defaults to erofs, unless the NixOS hardened profile is detected.
'';
};

storeDiskErofsFlags = mkOption {
type = with types; listOf str;
description = ''
Flags to pass to mkfs.erofs
Omit `"-Efragments"` and `"-Ededupe"` to enable multi-threading.
'';
default =
[ "-zlz4hc" ]
++
lib.optional (kernelAtLeast "5.16") "-Eztailpacking"
++
lib.optionals (kernelAtLeast "6.1") [
# not implemented with multi-threading
"-Efragments"
"-Ededupe"
];
defaultText = lib.literalExpression ''
[ "-zlz4hc" ]
++ lib.optional (kernelAtLeast "5.16") "-Eztailpacking"
++ lib.optionals (kernelAtLeast "6.1") [
"-Efragments"
"-Ededupe"
]
'';
};

storeDiskSquashfsFlags = mkOption {
type = with types; listOf str;
description = "Flags to pass to gensquashfs";
default = [ "-c" "zstd" "-j" "$NIX_BUILD_CORES" ];
};
};

config = lib.mkMerge [ {
Expand Down
46 changes: 5 additions & 41 deletions nixos-modules/microvm/store-disk.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,11 @@ let

in
{
options.microvm = with lib; {
storeDiskType = mkOption {
type = types.enum [ "squashfs" "erofs" ];
description = ''
Boot disk file system type: squashfs is smaller, erofs is supposed to be faster.
'';
};

storeDiskErofsFlags = mkOption {
type = with types; listOf str;
default =
[ "-zlz4hc" ]
++
lib.optional (kernelAtLeast "5.16") "-Eztailpacking"
++
lib.optionals (kernelAtLeast "6.1") [
# not implemented with multi-threading
"-Efragments"
"-Ededupe"
];
defaultText = lib.literalExpression ''
[ "-zlz4hc" ]
++ lib.optional (kernelAtLeast "5.16") "-Eztailpacking"
++ lib.optionals (kernelAtLeast "6.1") [
"-Efragments"
"-Ededupe"
]
'';
};

storeDiskSquashfsFlags = mkOption {
type = with types; listOf str;
default = [ "-c" "zstd" "-j" "$NIX_BUILD_CORES" ];
};

storeDisk = mkOption {
type = types.path;
description = ''
Generated
'';
};
options.microvm.storeDisk = with lib; mkOption {
type = types.path;
description = ''
Generated
'';
};

config = lib.mkMerge [
Expand Down

0 comments on commit 3394c37

Please sign in to comment.