Skip to content

Commit a93e682

Browse files
committed
refactor: simplify flake.nix structure and improve go binary derivation
1 parent 04aa42a commit a93e682

File tree

2 files changed

+75
-93
lines changed

2 files changed

+75
-93
lines changed

flake.lock

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,105 @@
11
{
22
description = "snitch - a friendlier ss/netstat for humans";
33

4-
inputs = {
5-
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
6-
systems.url = "github:nix-systems/default";
7-
};
4+
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
85

9-
outputs = { self, nixpkgs, systems }:
6+
outputs = { self, nixpkgs }:
107
let
11-
supportedSystems = import systems;
12-
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
8+
systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
9+
eachSystem = nixpkgs.lib.genAttrs systems;
1310

14-
# go 1.25 overlay (required until nixpkgs has it)
15-
goOverlay = final: prev:
11+
# go 1.25 binary derivation (required until nixpkgs ships it)
12+
mkGo125 = pkgs:
1613
let
1714
version = "1.25.0";
18-
platformInfo = {
19-
"x86_64-linux" = { suffix = "linux-amd64"; sri = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; };
20-
"aarch64-linux" = { suffix = "linux-arm64"; sri = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; };
21-
"x86_64-darwin" = { suffix = "darwin-amd64"; sri = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; };
22-
"aarch64-darwin" = { suffix = "darwin-arm64"; sri = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; };
23-
};
24-
hostSystem = prev.stdenv.hostPlatform.system;
25-
chosen = platformInfo.${hostSystem} or (throw "unsupported system: ${hostSystem}");
15+
platform = {
16+
"x86_64-linux" = { suffix = "linux-amd64"; hash = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; GOOS = "linux"; GOARCH = "amd64"; };
17+
"aarch64-linux" = { suffix = "linux-arm64"; hash = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; GOOS = "linux"; GOARCH = "arm64"; };
18+
"x86_64-darwin" = { suffix = "darwin-amd64"; hash = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; GOOS = "darwin"; GOARCH = "amd64"; };
19+
"aarch64-darwin" = { suffix = "darwin-arm64"; hash = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; GOOS = "darwin"; GOARCH = "arm64"; };
20+
}.${pkgs.stdenv.hostPlatform.system} or (throw "unsupported system: ${pkgs.stdenv.hostPlatform.system}");
2621
in
27-
{
28-
go_1_25 = prev.stdenvNoCC.mkDerivation {
29-
pname = "go";
30-
inherit version;
31-
src = prev.fetchurl {
32-
url = "https://go.dev/dl/go${version}.${chosen.suffix}.tar.gz";
33-
hash = chosen.sri;
34-
};
35-
dontBuild = true;
36-
installPhase = ''
37-
runHook preInstall
38-
mkdir -p "$out"/{bin,share}
39-
tar -C "$TMPDIR" -xzf "$src"
40-
cp -a "$TMPDIR/go" "$out/share/go"
41-
ln -s "$out/share/go/bin/go" "$out/bin/go"
42-
ln -s "$out/share/go/bin/gofmt" "$out/bin/gofmt"
43-
runHook postInstall
44-
'';
45-
dontPatchELF = true;
46-
dontStrip = true;
22+
pkgs.stdenv.mkDerivation {
23+
pname = "go";
24+
inherit version;
25+
src = pkgs.fetchurl {
26+
url = "https://go.dev/dl/go${version}.${platform.suffix}.tar.gz";
27+
inherit (platform) hash;
28+
};
29+
dontBuild = true;
30+
dontPatchELF = true;
31+
dontStrip = true;
32+
installPhase = ''
33+
runHook preInstall
34+
mkdir -p $out/{bin,share/go}
35+
tar -xzf $src --strip-components=1 -C $out/share/go
36+
ln -s $out/share/go/bin/go $out/bin/go
37+
ln -s $out/share/go/bin/gofmt $out/bin/gofmt
38+
runHook postInstall
39+
'';
40+
passthru = {
41+
inherit (platform) GOOS GOARCH;
4742
};
4843
};
49-
in
50-
{
51-
overlays.default = final: prev: {
52-
snitch = final.callPackage ./nix/package.nix { };
53-
};
5444

55-
packages = forAllSystems (system:
56-
let
57-
pkgs = import nixpkgs {
58-
inherit system;
59-
overlays = [ goOverlay ];
60-
};
61-
in
45+
pkgsFor = system: import nixpkgs { inherit system; };
46+
47+
mkSnitch = pkgs:
6248
let
6349
version = self.shortRev or self.dirtyShortRev or "dev";
50+
go = mkGo125 pkgs;
51+
buildGoModule = pkgs.buildGoModule.override { inherit go; };
6452
in
65-
{
66-
default = pkgs.buildGoModule {
67-
pname = "snitch";
68-
inherit version;
69-
src = self;
70-
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
71-
env.CGO_ENABLED = 0;
72-
ldflags = [
73-
"-s" "-w"
74-
"-X snitch/cmd.Version=${version}"
75-
"-X snitch/cmd.Commit=${version}"
76-
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
77-
];
78-
meta = with pkgs.lib; {
79-
description = "a friendlier ss/netstat for humans";
80-
homepage = "https://github.com/karol-broda/snitch";
81-
license = licenses.mit;
82-
platforms = platforms.linux;
83-
mainProgram = "snitch";
84-
};
53+
buildGoModule {
54+
pname = "snitch";
55+
inherit version;
56+
src = self;
57+
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
58+
env.CGO_ENABLED = "0";
59+
env.GOTOOLCHAIN = "local";
60+
ldflags = [
61+
"-s"
62+
"-w"
63+
"-X snitch/cmd.Version=${version}"
64+
"-X snitch/cmd.Commit=${version}"
65+
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
66+
];
67+
meta = {
68+
description = "a friendlier ss/netstat for humans";
69+
homepage = "https://github.com/karol-broda/snitch";
70+
license = pkgs.lib.licenses.mit;
71+
platforms = pkgs.lib.platforms.linux;
72+
mainProgram = "snitch";
8573
};
74+
};
75+
in
76+
{
77+
packages = eachSystem (system:
78+
let pkgs = pkgsFor system; in
79+
{
80+
default = mkSnitch pkgs;
81+
snitch = mkSnitch pkgs;
8682
}
8783
);
8884

89-
devShells = forAllSystems (system:
85+
devShells = eachSystem (system:
9086
let
91-
pkgs = import nixpkgs {
92-
inherit system;
93-
overlays = [ goOverlay ];
94-
};
87+
pkgs = pkgsFor system;
88+
go = mkGo125 pkgs;
9589
in
9690
{
9791
default = pkgs.mkShell {
98-
packages = [ pkgs.go_1_25 pkgs.git pkgs.vhs ];
99-
GOTOOLCHAIN = "local";
92+
packages = [ go pkgs.git pkgs.vhs ];
93+
env.GOTOOLCHAIN = "local";
10094
shellHook = ''
10195
echo "go toolchain: $(go version)"
10296
'';
10397
};
10498
}
10599
);
100+
101+
overlays.default = final: _prev: {
102+
snitch = mkSnitch final;
103+
};
106104
};
107105
}

0 commit comments

Comments
 (0)