From 5e9acb35b0da2ea7b4d673d0fb66a64fe110cf4b Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 15 Nov 2022 01:15:05 +0100 Subject: lib/mkSandboxSystem: print dirs and ports shared with host --- lib/mk-sandbox-system.nix | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/mk-sandbox-system.nix b/lib/mk-sandbox-system.nix index 56ccad1..5fde67e 100644 --- a/lib/mk-sandbox-system.nix +++ b/lib/mk-sandbox-system.nix @@ -7,14 +7,44 @@ flake: , envVars ? { } , restrictNetwork ? true # to be replaced with virtualisation.restrictNetwork , patchQemu9p ? false # until qemu 7.2.0 becomes available in nixpkgs -}: +}@params: let shellLib = flake.lib.shell { inherit pkgs; }; + print = rec { + printSharedDir = name: { source, target }: '' + echo -en ${shellLib.fmt.keyword target}": " + echo ${pkgs.lib.escapeShellArg source} + ''; + printSharedDirs = sharedDirs: '' + ${shellLib.fmt.printSectionTitle "SHARED DIRECTORIES (guest: host)"} + ${shellLib.mapAttrsToLines printSharedDir sharedDirs} + ''; + + formatAddrPort = { address, port }: "${address}:${toString port}"; + printForwardedPort = { from, proto, host, guest, }: + if from == "host" then '' + echo -n ${proto}' ' + echo -en ${shellLib.fmt.keyword (formatAddrPort host)} + echo -n ' -> ' + echo ${formatAddrPort guest} + '' else '' + echo -n ${proto}' ' + echo -n ${formatAddrPort host} + echo -n ' <- ' + echo -e ${shellLib.fmt.keyword (formatAddrPort guest)} + ''; + printForwardedPorts = portForwards: '' + ${shellLib.fmt.printSectionTitle "FORWARDED PORTS (host <-> guest)"} + ${pkgs.lib.concatMapStringsSep "\n" printForwardedPort portForwards} + ''; + }; + in rec { - nixosConfigurations.${name} = pkgs.nixos ({ modulesPath, lib, pkgs, ... }: { + nixosConfigurations.${name} = pkgs.nixos + ({ modulesPath, config, lib, pkgs, ... }: { imports = [ (modulesPath + "/profiles/minimal.nix") { environment.noXlibs = false; } # avoid mass rebuild @@ -22,7 +52,7 @@ in rec { (modulesPath + "/profiles/qemu-guest.nix") (modulesPath + "/virtualisation/qemu-vm.nix") - config + params.config ]; system.stateVersion = lib.mkDefault lib.trivial.release; @@ -52,12 +82,14 @@ in rec { 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 '' ${shellLib.ifSomeAttrs envVars shellLib.printEnvVars} ${shellLib.ifSomeList tools shellLib.printBins} - echo + + ${shellLib.ifSomeAttrs config.virtualisation.sharedDirectories + print.printSharedDirs} + ${shellLib.ifSomeList config.virtualisation.forwardPorts + print.printForwardedPorts} ''; }; -- cgit v1.2.3