From 0dafe2170deb6a691255cecc212af4b1cdf8297b Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 2 Jul 2023 01:11:16 +1000 Subject: [PATCH] Add `darwin-version` command --- flake.nix | 11 ++++--- modules/examples/flake/flake.nix | 3 ++ modules/examples/lnl.nix | 2 +- modules/nix/nix-darwin.nix | 5 ++-- modules/system/default.nix | 8 ++++- modules/system/version.nix | 6 ++++ pkgs/nix-tools/darwin-rebuild.sh | 5 ++-- pkgs/nix-tools/darwin-version.sh | 50 ++++++++++++++++++++++++++++++++ pkgs/nix-tools/default.nix | 7 +++++ 9 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 pkgs/nix-tools/darwin-version.sh diff --git a/flake.nix b/flake.nix index b1808c9d..d09f4d6c 100644 --- a/flake.nix +++ b/flake.nix @@ -30,14 +30,17 @@ system.checks.verifyNixPath = lib.mkDefault false; - system.darwinVersionSuffix = ".${self.shortRev or "dirty"}"; - system.darwinRevision = lib.mkIf (self ? rev) self.rev; + system.darwinVersionSuffix = ".${self.shortRev or self.dirtyShortRev or "dirty"}"; + system.darwinRevision = let + rev = self.rev or self.dirtyRev or null; + in + lib.mkIf (rev != null) rev; }) ]; }); }; overlays.default = final: prev: { - inherit (prev.callPackage ./pkgs/nix-tools { }) darwin-rebuild darwin-option; + inherit (prev.callPackage ./pkgs/nix-tools { }) darwin-rebuild darwin-option darwin-version; darwin-uninstaller = prev.callPackage ./pkgs/darwin-uninstaller { nix-darwin = self; }; }; @@ -76,7 +79,7 @@ in { default = self.packages.${system}.darwin-rebuild; - inherit (pkgs) darwin-option darwin-rebuild darwin-uninstaller; + inherit (pkgs) darwin-option darwin-rebuild darwin-version darwin-uninstaller; }); }; } diff --git a/modules/examples/flake/flake.nix b/modules/examples/flake/flake.nix index 02b445cc..86505629 100644 --- a/modules/examples/flake/flake.nix +++ b/modules/examples/flake/flake.nix @@ -27,6 +27,9 @@ programs.zsh.enable = true; # default shell on catalina # programs.fish.enable = true; + # Set Git commit hash for darwin-version. + system.configurationRevision = self.rev or self.dirtyRev or null; + # Used for backwards compatibility, please read the changelog before changing. # $ darwin-rebuild changelog system.stateVersion = 4; diff --git a/modules/examples/lnl.nix b/modules/examples/lnl.nix index 5184b98c..264bd1a1 100644 --- a/modules/examples/lnl.nix +++ b/modules/examples/lnl.nix @@ -125,7 +125,7 @@ set -g pane-border-style fg=black set -g status-bg black set -g status-fg white - set -g status-right '#[fg=white]#(id -un)@#(hostname) #(cat /run/current-system/darwin-version)' + set -g status-right '#[fg=white]#(id -un)@#(hostname) #(jq --raw-output '.darwinLabel' /run/current-system/darwin-version.json)' ''; environment.etc."nix/user-sandbox.sb".text = '' diff --git a/modules/nix/nix-darwin.nix b/modules/nix/nix-darwin.nix index ee17af26..7f07b458 100644 --- a/modules/nix/nix-darwin.nix +++ b/modules/nix/nix-darwin.nix @@ -7,7 +7,7 @@ let nixPackage = config.nix.package; }; - inherit (nix-tools) darwin-option darwin-rebuild; + inherit (nix-tools) darwin-option darwin-rebuild darwin-version; in { @@ -17,10 +17,11 @@ in [ # Include nix-tools by default darwin-option darwin-rebuild + darwin-version ]; system.build = { - inherit darwin-option darwin-rebuild; + inherit darwin-option darwin-rebuild darwin-version; }; }; diff --git a/modules/system/default.nix b/modules/system/default.nix index af90adab..0eb46a9d 100644 --- a/modules/system/default.nix +++ b/modules/system/default.nix @@ -96,6 +96,12 @@ in activationUserScript = cfg.activationScripts.userScript.text; inherit (cfg) darwinLabel; + darwinVersionJson = (pkgs.formats.json {}).generate "darwin-version.json" ( + filterAttrs (k: v: v != null) { + inherit (config.system) darwinRevision nixpkgsRevision configurationRevision darwinLabel; + } + ); + buildCommand = '' mkdir $out @@ -129,7 +135,7 @@ in echo -n "$systemConfig" > $out/systemConfig - echo -n "$darwinLabel" > $out/darwin-version + ln -s $darwinVersionJson $out/darwin-version.json echo -n "$system" > $out/system ${cfg.systemBuilderCommands} diff --git a/modules/system/version.nix b/modules/system/version.nix index e45f6678..3effb505 100644 --- a/modules/system/version.nix +++ b/modules/system/version.nix @@ -108,6 +108,12 @@ in else lib.trivial.revisionWithDefault null; description = lib.mdDoc "The nixpkgs git revision from which this configuration was built."; }; + + system.configurationRevision = mkOption { + type = types.nullOr types.str; + default = null; + description = lib.mdDoc "The Git revision of the top-level flake from which this configuration was built."; + }; }; config = { diff --git a/pkgs/nix-tools/darwin-rebuild.sh b/pkgs/nix-tools/darwin-rebuild.sh index 52c746a6..d8c66bdd 100644 --- a/pkgs/nix-tools/darwin-rebuild.sh +++ b/pkgs/nix-tools/darwin-rebuild.sh @@ -8,12 +8,11 @@ showSyntax() { echo "darwin-rebuild [--help] {edit | switch | activate | build | check | changelog}" >&2 echo " [--list-generations] [{--profile-name | -p} name] [--rollback]" >&2 echo " [{--switch-generation | -G} generation] [--verbose...] [-v...]" >&2 - echo " [-Q] [{--max-jobs | -j} number] [--cores number] [--dry-run]" >&1 + echo " [-Q] [{--max-jobs | -j} number] [--cores number] [--dry-run]" >&2 echo " [--keep-going] [-k] [--keep-failed] [-K] [--fallback] [--show-trace]" >&2 echo " [-I path] [--option name value] [--arg name value] [--argstr name value]" >&2 - echo " [--flake flake] [--update-input input flake] [--impure] [--recreate-lock-file]" + echo " [--flake flake] [--update-input input flake] [--impure] [--recreate-lock-file]" >&2 echo " [--no-update-lock-file] [--refresh] ..." >&2 - exec man darwin-rebuild exit 1 } diff --git a/pkgs/nix-tools/darwin-version.sh b/pkgs/nix-tools/darwin-version.sh new file mode 100644 index 00000000..e19443a4 --- /dev/null +++ b/pkgs/nix-tools/darwin-version.sh @@ -0,0 +1,50 @@ +#! @shell@ +set -e +set -o pipefail +export PATH=@path@:$PATH + +showSyntax() { + echo "darwin-version [--help|--darwin-revision|--nixpkgs-revision|--configuration-revision|--json]" >&2 +} + +case "$1" in + --help) + showSyntax + ;; + --darwin-revision) + revision="$(jq --raw-output '.darwinRevision // "null"' < /run/current-system/darwin-version.json)" + if [[ "$revision" == "null" ]]; then + echo "$0: nix-darwin commit hash is unknown" >&2 + exit 1 + fi + echo "$revision" + ;; + --nixpkgs-revision) + revision="$(jq --raw-output '.nixpkgsRevision // "null"' < /run/current-system/darwin-version.json)" + if [[ "$revision" == "null" ]]; then + echo "$0: Nixpkgs commit hash is unknown" >&2 + exit 1 + fi + echo "$revision" + ;; + --configuration-revision) + revision="$(jq --raw-output '.configurationRevision // "null"' < /run/current-system/darwin-version.json)" + if [[ "$revision" == "null" ]]; then + echo "$0: configuration commit hash is unknown" >&2 + exit 1 + fi + echo "$revision" + ;; + --json) + cat /run/current-system/darwin-version.json + ;; + *) + label="$(jq --raw-output '.darwinLabel // "null"' < /run/current-system/darwin-version.json)" + if [[ "$label" == "null" ]]; then + showSyntax + exit 1 + fi + echo "$label" + ;; +esac + diff --git a/pkgs/nix-tools/default.nix b/pkgs/nix-tools/default.nix index 22fcafa9..cb5d62f6 100644 --- a/pkgs/nix-tools/default.nix +++ b/pkgs/nix-tools/default.nix @@ -35,4 +35,11 @@ in inherit (stdenv) shell; } ./darwin-rebuild.sh; + + darwin-version = writeProgram "darwin-version" + { + inherit (stdenv) shell; + path = lib.makeBinPath [ jq ]; + } + ./darwin-version.sh; }