From de11e018dca18d11499debb8102ba6151cc21834 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 17 May 2025 13:39:56 +0200 Subject: modules/nixos-shell: Init A VM at your disposal. This is based on: https://github.com/Mic92/nixos-shell --- modules/by-name/ni/nixos-shell/nixos-shell.sh | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 modules/by-name/ni/nixos-shell/nixos-shell.sh (limited to 'modules/by-name/ni/nixos-shell/nixos-shell.sh') 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 . + +SHARED_DIR="$(mktemp --directory)" +cleanup() { + rm --recursive "$SHARED_DIR" +} +trap cleanup EXIT +export SHARED_DIR + +TMPDIR="$SHARED_DIR" +export TMPDIR + +cat <"$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" "$@" -- cgit 1.4.1