about summary refs log tree commit diff stats
path: root/modules/by-name/ni/nixos-shell/module.nix
diff options
context:
space:
mode:
Diffstat (limited to 'modules/by-name/ni/nixos-shell/module.nix')
-rw-r--r--modules/by-name/ni/nixos-shell/module.nix128
1 files changed, 128 insertions, 0 deletions
diff --git a/modules/by-name/ni/nixos-shell/module.nix b/modules/by-name/ni/nixos-shell/module.nix
new file mode 100644
index 00000000..219f080d
--- /dev/null
+++ b/modules/by-name/ni/nixos-shell/module.nix
@@ -0,0 +1,128 @@
+# 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>.
+{
+  lib,
+  config,
+  pkgs,
+  self,
+  ...
+}: let
+  cfg = config.soispha.nixos-shell;
+in {
+  options.soispha.nixos-shell = {
+    enable = lib.mkEnableOption "nixos-shell";
+
+    user_name = lib.mkOption {
+      type = lib.types.str;
+      default = "soispha";
+      description = "The user to auto-login into the vm.";
+    };
+
+    configuration = {
+      specialArgs = lib.mkOption {
+        type = lib.types.attrsOf lib.types.anything;
+        default = {};
+        description = ''
+          The arguments to pass to the `specialArgs` attribute set.
+        '';
+      };
+      value = lib.mkOption {
+        type = lib.types.deferredModule;
+        default = {};
+        description = ''
+          Additional NixOS configuration to load into the VM's config.
+        '';
+      };
+    };
+
+    mounts = lib.mkOption {
+      type = lib.types.attrsOf (lib.types.submodule ({config, ...}: {
+        options = {
+          target = lib.mkOption {
+            type = lib.types.path;
+            description = "Target on the guest.";
+          };
+
+          cache_mode = lib.mkOption {
+            type = lib.types.enum ["none" "loose" "fscache" "mmap"];
+            default = "loose"; # bad idea? Well, at least it is fast!1!!
+            description = "9p caching policy";
+          };
+
+          readOnly =
+            (lib.mkEnableOption "mount this disk in read-only mode")
+            // {
+              default = true;
+            };
+
+          tag = lib.mkOption {
+            type = lib.types.str;
+            internal = true;
+          };
+        };
+
+        config.tag = lib.mkDefault (
+          builtins.substring 0 31 ( # tags must be shorter than 32 bytes
+            "a"
+            + # tags must not begin with a digit
+            builtins.hashString "md5" config._module.args.name
+          )
+        );
+      }));
+      default = {};
+      description = ''
+        Extra paths to make available in the vm.
+        These will be mounted ro to their `target.`
+      '';
+    };
+  };
+
+  config = let
+    vmSystem = self.inputs.nixpkgs.lib.nixosSystem {
+      inherit (cfg.configuration) specialArgs;
+
+      modules = [
+        {
+          # TODO(@bpeetz): This should be bumped each release. <2025-05-17>
+          system.stateVersion = "25.05";
+        }
+
+        cfg.configuration.value
+
+        (import ./shell_setup.nix {inherit cfg;})
+      ];
+    };
+
+    nixos-shell = pkgs.writeShellApplication {
+      name = "nixos-shell";
+      text = builtins.readFile ./nixos-shell.sh;
+
+      # We need to keep the PATH, as we otherwise can't pass it along.
+      inheritPath = true;
+
+      runtimeInputs = [
+        vmSystem.config.system.build.vm
+        pkgs.mktemp
+        pkgs.coreutils
+        pkgs.moreutils # for sponge
+      ];
+      runtimeEnv = {
+        HOST_NAME = vmSystem.config.system.name;
+      };
+    };
+  in
+    lib.mkIf cfg.enable {
+      environment.systemPackages = [
+        nixos-shell
+      ];
+
+      system.build.nixos-shell = vmSystem.config.system.build.vm;
+    };
+}