From 06efd25d984317d03c05176b53f9d61ca14c805b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 11 May 2024 01:26:54 +0200 Subject: [PATCH] Allow mapping the current user's keys into the VM --- lib/configuration.nix | 1 + lib/modules/custom-build-vm.nix | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/configuration.nix b/lib/configuration.nix index 620fc48..02327b7 100644 --- a/lib/configuration.nix +++ b/lib/configuration.nix @@ -16,6 +16,7 @@ vmConfig = { virtualisation = { sharedHostKeys = true; + sharedUserKeys = true; usb-redirect = true; virt-viewer = true; cores = 4; diff --git a/lib/modules/custom-build-vm.nix b/lib/modules/custom-build-vm.nix index 422ce6c..1510b64 100644 --- a/lib/modules/custom-build-vm.nix +++ b/lib/modules/custom-build-vm.nix @@ -36,6 +36,12 @@ in { default = false; }; + sharedUserKeys = lib.mkOption { + type = lib.types.bool; + description = lib.mdDoc "Whether to load the current user's keys into every user's homer on the VM."; + default = false; + }; + usb-redirect = lib.mkOption { type = lib.types.bool; description = lib.mdDoc "Whether to enable USB redirection to the VM."; @@ -137,7 +143,21 @@ in { source = path; target = path; }; - }; + } // (lib.optionalAttrs vmVariant.virtualisation.sharedUserKeys ( + lib.attrsets.concatMapAttrs ( + name: user: + let + prefix = if vmVariant.virtualisation.runAsRoot + then "sudo -u#$SUDO_UID " + else ""; + homeDir = "$(${prefix}bash -c 'echo $HOME')"; + in { + "userKeys-${name}" = { + source = "${homeDir}/.ssh"; + target = "/home/${name}/.ssh"; + }; + }) + config.users.myUsers)); }; };