From 94b4db41750a7b6846ba57f643cb05d8bf94e1cb Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 14 Nov 2022 00:40:17 +0100 Subject: mkSandboxSystem: add tools and envVars option with printing --- example/flake.nix | 13 +++++++++++++ lib/mk-sandbox-system.nix | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/example/flake.nix b/example/flake.nix index 50931c6..17d15fc 100644 --- a/example/flake.nix +++ b/example/flake.nix @@ -50,6 +50,10 @@ # By default, the VM is launched in the current console without a graphical # interface, dropping to a shell for the default dummy user within. # + # The binaries made available through the `tools` option and the + # environment variable set through the `envVars` option are printed when + # entering the shell. + # # The current working directory from which the Flake is run is mounted and # made available within the virtual machine in /mnt. The root filesystem # is ephemeral (written to a temporary file in /tmp). @@ -64,6 +68,15 @@ name = "sandbox"; user = "dummy"; + tools = with pkgs; [ + postgresql + pgcli + ]; + + envVars = { + PGDATABASE = "app"; + }; + config = { virtualisation.forwardPorts = [ { from = "host"; host.port = 5432; guest.port = 5432; } # postgres diff --git a/lib/mk-sandbox-system.nix b/lib/mk-sandbox-system.nix index a56bdab..b678399 100644 --- a/lib/mk-sandbox-system.nix +++ b/lib/mk-sandbox-system.nix @@ -3,11 +3,16 @@ flake: , name ? "sandbox" , user ? "dummy" , config ? { } +, tools ? [] +, envVars ? { } , restrictNetwork ? true # to be replaced with virtualisation.restrictNetwork , patchQemu9p ? false # until qemu 7.2.0 becomes available in nixpkgs }: -rec { +let + shellLib = flake.lib.shell { inherit pkgs; }; + +in rec { nixosConfigurations.${name} = pkgs.nixos ({ modulesPath, lib, pkgs, ... }: { imports = [ @@ -43,6 +48,19 @@ rec { ''; }; + environment = { + variables = envVars; + systemPackages = tools; + + # TODO: also print a summary of the host mount points + # TODO: also print a summary of the forwarded ports + interactiveShellInit = lib.mkBefore '' + ${lib.optionalString (envVars != {}) (shellLib.printEnvVars envVars)} + ${lib.optionalString (tools != []) (shellLib.printBins tools)} + echo + ''; + }; + virtualisation = { graphics = lib.mkDefault false; diskImage = lib.mkDefault "$TMP_DISK"; -- cgit v1.2.3