summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authornzbr <mail@nzbr.de>2022-12-17 14:18:47 +0100
committernzbr <mail@nzbr.de>2022-12-17 14:36:48 +0100
commit8d295219a6dd48fdd6d45b2ccec1467986911fa8 (patch)
tree5ad6959183b1cfd147ce11cd0ec76ea842459770 /modules
parentfab2833c091e059fd75e0c2cd570279500e76351 (diff)
backport nixos-wsl-version
Diffstat (limited to 'modules')
-rw-r--r--modules/version.nix79
1 files changed, 79 insertions, 0 deletions
diff --git a/modules/version.nix b/modules/version.nix
new file mode 100644
index 0000000..01a8be6
--- /dev/null
+++ b/modules/version.nix
@@ -0,0 +1,79 @@
+{ lib, pkgs, options, config, ... }:
+with builtins; with lib;
+{
+
+ options = with types; {
+ wsl.version =
+ let
+ versionFile = splitString "\n" (readFile ../VERSION);
+ in
+ {
+ release = mkOption {
+ description = "the NixOS-WSL release";
+ type = str;
+ default = elemAt versionFile 0;
+ };
+ rev = mkOption {
+ description = "the NixOS-WSL git revision";
+ type = str;
+ default = elemAt versionFile 1;
+ };
+ systemd = mkOption {
+ 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 '${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}
+ ''
+ )
+ ];
+
+ };
+
+}