From 039fde18b227e3330e870c084be295f7412d7af6 Mon Sep 17 00:00:00 2001 From: Darwin Wu Date: Wed, 21 Oct 2026 07:15:08 +0000 Subject: [PATCH] build: make rfdtool installable from the flake in other repositories RFD repositories can now take rfdtool as a flake input and put the binary in their dev shell, through packages.${system}.default (or .rfdtool) or overlays.default, which adds pkgs.rfdtool. Both build from one definition with the consumer's nixpkgs when it follows theirs, using its go_1_26. The binary is stripped and reports the commit it was built from with rfdtool --version; go install builds report the module version instead. apps.default is gone: nix run uses the package's meta.mainProgram. The README shows the flake input, with git beside rfdtool for check --base-ref and new. Amp-Thread-ID: https://ampcode.com/threads/T-01a15d15-bb12-7429-9794-374e145cd95e Co-authored-by: Amp --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- flake.nix | 54 ++++++++++++++++++++++++++++++++++++++---------------- main.go | 22 ++++++++++++++++++++-- 3 files changed, 105 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 53acf41..c7b6688 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,53 @@ To pin the version a repository uses, add it as a Go tool and run it with go get -tool github.com/chaba-dev/rfdtool@latest ``` -With Nix, run it with `nix run github:chaba-dev/rfdtool -- check`, or add -the flake's `packages.default` to a dev shell. +### With Nix + +Run it once with `nix run github:chaba-dev/rfdtool -- check`, or install it +with `nix profile install github:chaba-dev/rfdtool`. + +In an RFD repository with a flake, add rfdtool as an input and put the binary +in the dev shell, so `rfdtool` is on the path there and in CI's +`nix develop --command`. `flake.lock` pins the version; `nix flake update +rfdtool` moves it to the latest commit. + +```nix +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + rfdtool = { + url = "github:chaba-dev/rfdtool"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + { nixpkgs, flake-utils, rfdtool, ... }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + packages = [ + rfdtool.packages.${system}.default + pkgs.git # for rfdtool check --base-ref, and authors in rfdtool new + ]; + }; + } + ); +} +``` + +A flake that builds its own nixpkgs can add `rfdtool.overlays.default` to its +overlays and use `pkgs.rfdtool` instead. Either way the package needs Go 1.26 +(`go_1_26`) from that nixpkgs. It builds on Linux and Apple Silicon macOS; +current nixpkgs no longer supports Intel Macs. + +`rfdtool --version` prints the commit a Nix build came from, or the module +version for `go install`. To start a project, create the RFD directory and the first RFD: diff --git a/flake.nix b/flake.nix index 9533650..1e2a4ed 100644 --- a/flake.nix +++ b/flake.nix @@ -13,35 +13,57 @@ flake-utils, ... }: - flake-utils.lib.eachDefaultSystem ( - system: - let - pkgs = import nixpkgs { inherit system; }; - - # The Go release go.mod asks for, used to build and in the dev shell. - go = pkgs.go_1_26; - buildGoModule = pkgs.buildGoModule.override { inherit go; }; - - rfdtool = buildGoModule { - pname = "rfdtool"; + let + # rfdtool built with the Go release go.mod asks for, from any nixpkgs + # that has it. + mkRfdtool = + pkgs: + let version = self.shortRev or self.dirtyShortRev or "dev"; - src = ./.; + buildGoModule = pkgs.buildGoModule.override { go = pkgs.go_1_26; }; + in + buildGoModule { + pname = "rfdtool"; + inherit version; + src = self; # Update when go.mod or go.sum change: set it to pkgs.lib.fakeHash, # run nix build, and copy the hash it reports. vendorHash = "sha256-3+raHHPQE20lsZ09Kvljd9FVECCN9q7ZQlKk6KemRig="; + ldflags = [ + "-s" + "-w" + "-X main.version=${version}" + ]; # The tests read and write git repositories. nativeCheckInputs = [ pkgs.git ]; - meta.mainProgram = "rfdtool"; + meta = { + description = "Check, link, start, and report on Requests for Discussion"; + homepage = "https://github.com/chaba-dev/rfdtool"; + mainProgram = "rfdtool"; + }; }; + in + { + # Adds pkgs.rfdtool, for flakes that build their own nixpkgs. + overlays.default = final: prev: { rfdtool = mkRfdtool final; }; + } + // flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { inherit system; }; + go = pkgs.go_1_26; + rfdtool = mkRfdtool pkgs; in { - packages.default = rfdtool; - - apps.default = flake-utils.lib.mkApp { drv = rfdtool; }; + packages = { + inherit rfdtool; + default = rfdtool; + }; # nix flake check builds the package, which runs go test. checks.default = rfdtool; + # For working on rfdtool itself, with the Go release go.mod asks for. devShells.default = pkgs.mkShell { packages = [ go diff --git a/main.go b/main.go index c149bb5..28dacf8 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime/debug" "strings" "github.com/urfave/cli/v3" @@ -16,10 +17,27 @@ import ( "github.com/chaba-dev/rfdtool/internal/rfd" ) +// version is set at build time with -ldflags "-X main.version=...", as the +// Nix flake does. +var version = "" + +// buildVersion returns the version set at build time, or the module version +// go install records, such as v0.1.0. +func buildVersion() string { + if version != "" { + return version + } + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" { + return info.Main.Version + } + return "(devel)" +} + func main() { cmd := &cli.Command{ - Name: "rfdtool", - Usage: "tools for a project's Requests for Discussion", + Name: "rfdtool", + Version: buildVersion(), + Usage: "tools for a project's Requests for Discussion", Flags: []cli.Flag{ &cli.StringFlag{ Name: "config",