summaryrefslogtreecommitdiff
path: root/modules/version.nix
blob: 79683180bf34d68d639b57d8697fea3ad0a15ae6 (plain)
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
{ lib, pkgs, options, config, ... }:
with lib;
{

  options = with types; {
    wsl.version =
      let
        versionFile = splitString "\n" (readFile ../VERSION);
      in
      {
        release = mkOption {
          internal = true;
          description = "the NixOS-WSL release";
          type = str;
          default = elemAt versionFile 0;
        };
        rev = mkOption {
          internal = true;
          description = "the NixOS-WSL git revision";
          type = str;
          default = elemAt versionFile 1;
        };
        systemd = mkOption {
          internal = true;
          type = enum [ "native" "syschdemd" ];
          description = "the systemd implementation used by NixOS-WSL";
          default = if config.wsl.nativeSystemd then "native" else "syschdemd";
        };
      };
  };

  config = mkIf config.wsl.enable {

    environment.systemPackages = [
      (
        with config.wsl.version;
        let
          opts = options.wsl.version;
          maxlen = foldl (acc: opt: max acc (stringLength opt)) 0 ((attrNames opts) ++ [ "help" "json" ]);
          rightPad = text: "${text}${fixedWidthString (2 + maxlen - (stringLength text)) " " ""}";
        in
        pkgs.writeShellScriptBin "nixos-wsl-version" ''
          for arg in "$@"; do
            case $arg in
              --help)
                echo "Usage: nixos-wsl-version [option]"
                echo "Options:"
                echo -e "  --${rightPad "help"}Show this help message"
                ${concatStringsSep "\n" (
                  mapAttrsToList (option: value: ''
                    echo "  --${rightPad option}Show ${value.description}"
                  '') opts
                )}
                echo -e "  --${rightPad "json"}Show everything in JSON format"
                exit 0
                ;;
              ${concatStringsSep "\n" (
                mapAttrsToList (option: value: ''
                  --${option})
                  echo ${value}
                  exit 0
                  ;;
                '') config.wsl.version
              )}
              --json)
                echo '${generators.toJSON {} config.wsl.version}' | ${pkgs.jq}/bin/jq # Use jq to pretty-print the JSON
                exit 0
                ;;
              *)
                echo "Unknown argument: $arg"
                exit 1
                ;;
            esac
          done
          echo NixOS-WSL ${config.wsl.version.release} ${config.wsl.version.rev} ${config.wsl.version.systemd}
        ''
      )
    ];

  };

}