about summary refs log tree commit diff stats
path: root/modules/by-name/ni/nixos-shell/nixos-shell.sh
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-05-17 13:39:56 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-05-17 13:39:56 +0200
commitde11e018dca18d11499debb8102ba6151cc21834 (patch)
tree22c06529d68394cd19ec7792f5e71c6f2a230b4a /modules/by-name/ni/nixos-shell/nixos-shell.sh
parentpkgs/default.nix: Migrate to the package arguments (diff)
downloadnixos-config-de11e018dca18d11499debb8102ba6151cc21834.zip
modules/nixos-shell: Init
A VM at your disposal.

This is based on: https://github.com/Mic92/nixos-shell
Diffstat (limited to 'modules/by-name/ni/nixos-shell/nixos-shell.sh')
-rwxr-xr-xmodules/by-name/ni/nixos-shell/nixos-shell.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/modules/by-name/ni/nixos-shell/nixos-shell.sh b/modules/by-name/ni/nixos-shell/nixos-shell.sh
new file mode 100755
index 00000000..390e60b1
--- /dev/null
+++ b/modules/by-name/ni/nixos-shell/nixos-shell.sh
@@ -0,0 +1,60 @@
+#!/usr/bin/env dash
+
+# nixos-config - My current NixOS configuration
+#
+# Copyright (C) 2025 Jörg Thalheim and contributors
+# SPDX-License-Identifier: MIT
+#
+# This file is part of my nixos-config.
+#
+# You should have received a copy of the License along with this program.
+# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+SHARED_DIR="$(mktemp --directory)"
+cleanup() {
+    rm --recursive "$SHARED_DIR"
+}
+trap cleanup EXIT
+export SHARED_DIR
+
+TMPDIR="$SHARED_DIR"
+export TMPDIR
+
+cat <<EOF >"$SHARED_DIR/.env_variables"
+{
+    "pwd": "$PWD",
+    "term": "$TERM",
+    "path": "$PATH"
+}
+EOF
+
+mk_tag() {
+    additional_path="$1"
+
+    # tags must be shorter than 32 bytes
+    # and must not begin with a digit.
+    {
+        printf "a"
+        echo "$additional_path" | sha256sum | head -c 30
+    }
+}
+
+for raw_additional_path in "$@"; do
+    additional_path="$(realpath "$raw_additional_path")"
+    tag="$(mk_tag "$additional_path")"
+
+    if [ "$(jq --arg mount "$tag" '.mount.[$mount]' "$SHARED_DIR/.env_variables")" != "null" ]; then
+        echo "Path '$additional_path' alread added."
+        shift 1
+        continue
+    fi
+
+    jq --arg mount "$tag" --arg target "$additional_path" \
+        '. + {mount: {$mount: $target}}' "$SHARED_DIR/.env_variables" | sponge "$SHARED_DIR/.env_variables"
+
+    set -- "$@" -virtfs "local,path=$additional_path,security_model=none,readonly=on,mount_tag=$tag"
+    shift 1
+done
+
+# Do not exec here, as we don't want to lose our cleanup hooks.
+"run-$HOST_NAME-vm" "$@"