flake.nix: add run-workflow-with-vm app

This commit is contained in:
Gabriel Arazas 2024-01-05 20:20:37 +08:00
parent e3b8f8bfce
commit 2d7e8147e1
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
6 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
nix-build -A config.system.build.vm -k \
--argstr workflow "$1" \
@datadir@/@projectname@/configuration.nix \
@inputsArgs@ \
${NIX_EXTRA_ARGS[@]}

View File

@ -0,0 +1,72 @@
{ workflow }:
let
pkgs = import <nixpkgs> { };
config' = import <config> { inherit pkgs; };
lib = pkgs.lib.extend (self: super:
let
publicLib = import <config/lib> { lib = super; };
in
{
inherit (publicLib) countAttrs getSecrets attachSopsPathPrefix;
# Until I figure out how to properly add them only for their respective
# environment, this is the working solution for now. Not really perfect
# since we use one nixpkgs instance for each configuration (home-manager or
# otherwise).
private = publicLib
// import <config/lib/private.nix> { lib = self; }
// import <config/lib/home-manager.nix> { lib = self; };
});
modules = import <config/modules/nixos> { inherit lib; isInternal = true; };
hmModules = import <config/modules/home-manager> { inherit lib; isInternal = true; };
in
import <nixpkgs/nixos/lib/eval-config.nix> {
inherit lib;
modules = modules ++ [
<home-manager/nixos>
<nixos-generators/formats/vm.nix>
<nixos-generators/format-module.nix>
({ config, lib, pkgs, ... }: {
imports = [
(
let
password = "nixos";
in
lib.private.mapHomeManagerUser "alice" {
inherit password;
extraGroups = [
"wheel"
];
description = "The password is '${password}'";
isNormalUser = true;
createHome = true;
home = "/home/alice";
}
)
];
config = {
home-manager.sharedModules = hmModules;
_module.args = {
nix-colors = import <nix-colors> { };
};
virtualisation.qemu.options = [
"-vga virtio"
"-display gtk,gl=on"
];
workflows.workflows.${workflow}.enable = true;
nixpkgs.overlays = [
config'.overlays.default
];
system.stateVersion = "23.11";
};
})
];
}

View File

@ -0,0 +1,30 @@
{ stdenv
, lib
, meson
, ninja
, makeWrapper
, inputs ? []
}:
stdenv.mkDerivation {
pname = "run-workflow-with-vm";
version = "2024-01-05";
src = ./.;
nativeBuildInputs = [
meson
ninja
makeWrapper
];
preConfigure = ''
mesonFlagsArray+=("-Dinputs=[${lib.concatStringsSep "," inputs}]")
'';
meta = with lib; {
description = "Quickly run workflow modules with a VM.";
license = licenses.gpl3Plus;
platforms = platforms.linux;
};
}

View File

@ -0,0 +1,33 @@
project('run-workflow-with-vm',
version: '2024-01-05',
license: 'GPL-3.0-or-later',
meson_version: '>=0.54.0',
)
prefix = get_option('prefix')
datadir = join_paths(prefix, get_option('datadir'))
sysconfdir = join_paths(prefix, get_option('sysconfdir'))
includedInputs = get_option('inputs')
inputsArgs = ''
foreach input : includedInputs
inputsArgs += '-I ' + input + ' \\\n '
endforeach
bindata = configuration_data()
bindata.set('datadir', datadir)
bindata.set('inputsArgs', inputsArgs)
bindata.set('projectname', 'run-workflow-with-vm')
configure_file(
input: 'app.sh',
output: 'run-workflow-with-vm',
configuration: bindata,
install_dir: get_option('bindir'),
install_mode: 'rwxr-xr-x',
install: true
)
install_data(
'./configuration.nix',
install_mode: 'r--r--r--'
)

View File

@ -0,0 +1,5 @@
option('inputs',
type: 'array',
value: [],
description: 'A list of inputs to be included in NIX_PATH.'
)

View File

@ -365,6 +365,29 @@
};
in
{
apps = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
in
{
run-workflow-with-vm =
let
inputsArgs = lib.mapAttrsToList
(name: source:
let
name' = if (name == "self") then "config" else name;
in
"'${name'}=${source}'")
inputs;
script = pkgs.callPackage ./apps/run-workflow-with-vm {
inputs = inputsArgs;
};
in
{
type = "app";
program = "${script}/bin/run-workflow-with-vm";
};
});
# Exposes only my library with the custom functions to make it easier to
# include in other flakes for whatever reason may be.
lib = import ./lib { lib = nixpkgs.lib; };