From 8d295219a6dd48fdd6d45b2ccec1467986911fa8 Mon Sep 17 00:00:00 2001 From: nzbr Date: Sat, 17 Dec 2022 14:18:47 +0100 Subject: backport nixos-wsl-version --- modules/version.nix | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 modules/version.nix (limited to 'modules') 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} + '' + ) + ]; + + }; + +} -- cgit v1.2.3