From e26b348689f64832dee634dea20f4bbf76340aed Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Thu, 10 Sep 2020 23:12:58 -0700 Subject: [PATCH] nixos-rebuild: add flake support for build-vm This relies on users using `nixpkgs.lib.nixosSystem` to define their system; otherwise, the `vm` and `vmWithBootLoader` attributes will not exist. --- flake.nix | 26 ++++++++++++++++--- .../modules/installer/tools/nixos-rebuild.sh | 10 ++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index e7c04417ac81..8440c460b16c 100644 --- a/flake.nix +++ b/flake.nix @@ -5,7 +5,6 @@ outputs = { self }: let - jobs = import ./pkgs/top-level/release.nix { nixpkgs = self; }; @@ -28,10 +27,31 @@ lib = lib.extend (final: prev: { nixosSystem = { modules, ... } @ args: import ./nixos/lib/eval-config.nix (args // { - modules = modules ++ - [ { system.nixos.versionSuffix = + modules = + let + vmConfig = (import ./nixos/lib/eval-config.nix + (args // { + modules = modules ++ [ ./nixos/modules/virtualisation/qemu-vm.nix ]; + })).config; + + vmWithBootLoaderConfig = (import ./nixos/lib/eval-config.nix + (args // { + modules = modules ++ [ + ./nixos/modules/virtualisation/qemu-vm.nix + { virtualisation.useBootLoader = true; } + ]; + })).config; + in + modules ++ [ + { + system.nixos.versionSuffix = ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified)}.${self.shortRev or "dirty"}"; system.nixos.revision = final.mkIf (self ? rev) self.rev; + + system.build = { + vm = vmConfig.system.build.vm; + vmWithBootLoader = vmWithBootLoaderConfig.system.build.vm; + }; } ]; }); diff --git a/nixos/modules/installer/tools/nixos-rebuild.sh b/nixos/modules/installer/tools/nixos-rebuild.sh index ad40fd2811dc..909e8b229c8a 100644 --- a/nixos/modules/installer/tools/nixos-rebuild.sh +++ b/nixos/modules/installer/tools/nixos-rebuild.sh @@ -438,15 +438,17 @@ if [ -z "$rollback" ]; then if [[ -z $flake ]]; then pathToConfig="$(nixBuild '' -A vm -k "${extraBuildFlags[@]}")" else - echo "$0: 'build-vm' is not supported with '--flake'" >&2 - exit 1 + nix "${flakeFlags[@]}" build "$flake#$flakeAttr.config.system.build.vm" \ + "${extraBuildFlags[@]}" "${lockFlags[@]}" + pathToConfig="$(readlink -f ./result)" fi elif [ "$action" = build-vm-with-bootloader ]; then if [[ -z $flake ]]; then pathToConfig="$(nixBuild '' -A vmWithBootLoader -k "${extraBuildFlags[@]}")" else - echo "$0: 'build-vm-with-bootloader' is not supported with '--flake'" >&2 - exit 1 + nix "${flakeFlags[@]}" build "$flake#$flakeAttr.config.system.build.vmWithBootLoader" \ + "${extraBuildFlags[@]}" "${lockFlags[@]}" + pathToConfig="$(readlink -f ./result)" fi else showSyntax