refactor: simplify flake.nix structure and improve go binary derivation

This commit is contained in:
Karol Broda
2025-12-21 01:57:32 +01:00
parent 04aa42a9c9
commit a93e682aa2
2 changed files with 77 additions and 95 deletions

18
flake.lock generated
View File

@@ -18,23 +18,7 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs"
"systems": "systems"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
} }
} }
}, },

154
flake.nix
View File

@@ -1,107 +1,105 @@
{ {
description = "snitch - a friendlier ss/netstat for humans"; description = "snitch - a friendlier ss/netstat for humans";
inputs = { inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
systems.url = "github:nix-systems/default";
};
outputs = { self, nixpkgs, systems }: outputs = { self, nixpkgs }:
let let
supportedSystems = import systems; systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system); eachSystem = nixpkgs.lib.genAttrs systems;
# go 1.25 overlay (required until nixpkgs has it) # go 1.25 binary derivation (required until nixpkgs ships it)
goOverlay = final: prev: mkGo125 = pkgs:
let let
version = "1.25.0"; version = "1.25.0";
platformInfo = { platform = {
"x86_64-linux" = { suffix = "linux-amd64"; sri = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; }; "x86_64-linux" = { suffix = "linux-amd64"; hash = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; GOOS = "linux"; GOARCH = "amd64"; };
"aarch64-linux" = { suffix = "linux-arm64"; sri = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; }; "aarch64-linux" = { suffix = "linux-arm64"; hash = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; GOOS = "linux"; GOARCH = "arm64"; };
"x86_64-darwin" = { suffix = "darwin-amd64"; sri = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; }; "x86_64-darwin" = { suffix = "darwin-amd64"; hash = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; GOOS = "darwin"; GOARCH = "amd64"; };
"aarch64-darwin" = { suffix = "darwin-arm64"; sri = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; }; "aarch64-darwin" = { suffix = "darwin-arm64"; hash = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; GOOS = "darwin"; GOARCH = "arm64"; };
}; }.${pkgs.stdenv.hostPlatform.system} or (throw "unsupported system: ${pkgs.stdenv.hostPlatform.system}");
hostSystem = prev.stdenv.hostPlatform.system;
chosen = platformInfo.${hostSystem} or (throw "unsupported system: ${hostSystem}");
in in
{ pkgs.stdenv.mkDerivation {
go_1_25 = prev.stdenvNoCC.mkDerivation { pname = "go";
pname = "go"; inherit version;
inherit version; src = pkgs.fetchurl {
src = prev.fetchurl { url = "https://go.dev/dl/go${version}.${platform.suffix}.tar.gz";
url = "https://go.dev/dl/go${version}.${chosen.suffix}.tar.gz"; inherit (platform) hash;
hash = chosen.sri; };
}; dontBuild = true;
dontBuild = true; dontPatchELF = true;
installPhase = '' dontStrip = true;
runHook preInstall installPhase = ''
mkdir -p "$out"/{bin,share} runHook preInstall
tar -C "$TMPDIR" -xzf "$src" mkdir -p $out/{bin,share/go}
cp -a "$TMPDIR/go" "$out/share/go" tar -xzf $src --strip-components=1 -C $out/share/go
ln -s "$out/share/go/bin/go" "$out/bin/go" ln -s $out/share/go/bin/go $out/bin/go
ln -s "$out/share/go/bin/gofmt" "$out/bin/gofmt" ln -s $out/share/go/bin/gofmt $out/bin/gofmt
runHook postInstall runHook postInstall
''; '';
dontPatchELF = true; passthru = {
dontStrip = true; inherit (platform) GOOS GOARCH;
};
};
pkgsFor = system: import nixpkgs { inherit system; };
mkSnitch = pkgs:
let
version = self.shortRev or self.dirtyShortRev or "dev";
go = mkGo125 pkgs;
buildGoModule = pkgs.buildGoModule.override { inherit go; };
in
buildGoModule {
pname = "snitch";
inherit version;
src = self;
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
env.CGO_ENABLED = "0";
env.GOTOOLCHAIN = "local";
ldflags = [
"-s"
"-w"
"-X snitch/cmd.Version=${version}"
"-X snitch/cmd.Commit=${version}"
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
];
meta = {
description = "a friendlier ss/netstat for humans";
homepage = "https://github.com/karol-broda/snitch";
license = pkgs.lib.licenses.mit;
platforms = pkgs.lib.platforms.linux;
mainProgram = "snitch";
}; };
}; };
in in
{ {
overlays.default = final: prev: { packages = eachSystem (system:
snitch = final.callPackage ./nix/package.nix { }; let pkgs = pkgsFor system; in
};
packages = forAllSystems (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ goOverlay ];
};
in
let
version = self.shortRev or self.dirtyShortRev or "dev";
in
{ {
default = pkgs.buildGoModule { default = mkSnitch pkgs;
pname = "snitch"; snitch = mkSnitch pkgs;
inherit version;
src = self;
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
env.CGO_ENABLED = 0;
ldflags = [
"-s" "-w"
"-X snitch/cmd.Version=${version}"
"-X snitch/cmd.Commit=${version}"
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
];
meta = with pkgs.lib; {
description = "a friendlier ss/netstat for humans";
homepage = "https://github.com/karol-broda/snitch";
license = licenses.mit;
platforms = platforms.linux;
mainProgram = "snitch";
};
};
} }
); );
devShells = forAllSystems (system: devShells = eachSystem (system:
let let
pkgs = import nixpkgs { pkgs = pkgsFor system;
inherit system; go = mkGo125 pkgs;
overlays = [ goOverlay ];
};
in in
{ {
default = pkgs.mkShell { default = pkgs.mkShell {
packages = [ pkgs.go_1_25 pkgs.git pkgs.vhs ]; packages = [ go pkgs.git pkgs.vhs ];
GOTOOLCHAIN = "local"; env.GOTOOLCHAIN = "local";
shellHook = '' shellHook = ''
echo "go toolchain: $(go version)" echo "go toolchain: $(go version)"
''; '';
}; };
} }
); );
overlays.default = final: _prev: {
snitch = mkSnitch final;
};
}; };
} }