aboutsummaryrefslogtreecommitdiff
path: root/lib/mk-sandbox-system.nix
diff options
context:
space:
mode:
authorpacien2022-11-15 01:15:05 +0100
committerpacien2022-11-15 01:15:05 +0100
commit5e9acb35b0da2ea7b4d673d0fb66a64fe110cf4b (patch)
tree9ac6ad87f0019f31c187b4f8ce861dc8cb0f100e /lib/mk-sandbox-system.nix
parentc2b18e3f862bdb61486d8027de4d8dd5b6114012 (diff)
downloadflaky-utils-5e9acb35b0da2ea7b4d673d0fb66a64fe110cf4b.tar.gz
lib/mkSandboxSystem: print dirs and ports shared with host
Diffstat (limited to 'lib/mk-sandbox-system.nix')
-rw-r--r--lib/mk-sandbox-system.nix44
1 files changed, 38 insertions, 6 deletions
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:
7, envVars ? { } 7, envVars ? { }
8, restrictNetwork ? true # to be replaced with virtualisation.restrictNetwork 8, restrictNetwork ? true # to be replaced with virtualisation.restrictNetwork
9, patchQemu9p ? false # until qemu 7.2.0 becomes available in nixpkgs 9, patchQemu9p ? false # until qemu 7.2.0 becomes available in nixpkgs
10}: 10}@params:
11 11
12let 12let
13 shellLib = flake.lib.shell { inherit pkgs; }; 13 shellLib = flake.lib.shell { inherit pkgs; };
14 14
15 print = rec {
16 printSharedDir = name: { source, target }: ''
17 echo -en ${shellLib.fmt.keyword target}": "
18 echo ${pkgs.lib.escapeShellArg source}
19 '';
20 printSharedDirs = sharedDirs: ''
21 ${shellLib.fmt.printSectionTitle "SHARED DIRECTORIES (guest: host)"}
22 ${shellLib.mapAttrsToLines printSharedDir sharedDirs}
23 '';
24
25 formatAddrPort = { address, port }: "${address}:${toString port}";
26 printForwardedPort = { from, proto, host, guest, }:
27 if from == "host" then ''
28 echo -n ${proto}' '
29 echo -en ${shellLib.fmt.keyword (formatAddrPort host)}
30 echo -n ' -> '
31 echo ${formatAddrPort guest}
32 '' else ''
33 echo -n ${proto}' '
34 echo -n ${formatAddrPort host}
35 echo -n ' <- '
36 echo -e ${shellLib.fmt.keyword (formatAddrPort guest)}
37 '';
38 printForwardedPorts = portForwards: ''
39 ${shellLib.fmt.printSectionTitle "FORWARDED PORTS (host <-> guest)"}
40 ${pkgs.lib.concatMapStringsSep "\n" printForwardedPort portForwards}
41 '';
42 };
43
15in rec { 44in rec {
16 45
17 nixosConfigurations.${name} = pkgs.nixos ({ modulesPath, lib, pkgs, ... }: { 46 nixosConfigurations.${name} = pkgs.nixos
47 ({ modulesPath, config, lib, pkgs, ... }: {
18 imports = [ 48 imports = [
19 (modulesPath + "/profiles/minimal.nix") 49 (modulesPath + "/profiles/minimal.nix")
20 { environment.noXlibs = false; } # avoid mass rebuild 50 { environment.noXlibs = false; } # avoid mass rebuild
@@ -22,7 +52,7 @@ in rec {
22 (modulesPath + "/profiles/qemu-guest.nix") 52 (modulesPath + "/profiles/qemu-guest.nix")
23 (modulesPath + "/virtualisation/qemu-vm.nix") 53 (modulesPath + "/virtualisation/qemu-vm.nix")
24 54
25 config 55 params.config
26 ]; 56 ];
27 57
28 system.stateVersion = lib.mkDefault lib.trivial.release; 58 system.stateVersion = lib.mkDefault lib.trivial.release;
@@ -52,12 +82,14 @@ in rec {
52 variables = envVars; 82 variables = envVars;
53 systemPackages = tools; 83 systemPackages = tools;
54 84
55 # TODO: also print a summary of the host mount points
56 # TODO: also print a summary of the forwarded ports
57 interactiveShellInit = lib.mkBefore '' 85 interactiveShellInit = lib.mkBefore ''
58 ${shellLib.ifSomeAttrs envVars shellLib.printEnvVars} 86 ${shellLib.ifSomeAttrs envVars shellLib.printEnvVars}
59 ${shellLib.ifSomeList tools shellLib.printBins} 87 ${shellLib.ifSomeList tools shellLib.printBins}
60 echo 88
89 ${shellLib.ifSomeAttrs config.virtualisation.sharedDirectories
90 print.printSharedDirs}
91 ${shellLib.ifSomeList config.virtualisation.forwardPorts
92 print.printForwardedPorts}
61 ''; 93 '';
62 }; 94 };
63 95