about summary refs log tree commit diff stats
path: root/modules/by-name/ni/nixos-shell/module.nix
blob: 219f080dda0005a3593ce76e4fdb5d53a52aab80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
    };
}