Skip to content

Commit

Permalink
Add pistol-static compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
doronbehar committed Nov 27, 2021
1 parent 222dee1 commit 1cabbce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
11 changes: 6 additions & 5 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 43 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
flake = false;
};
inputs.gomod2nix = {
url = "github:tweag/gomod2nix";
# For static compilation I need: https://github.com/tweag/gomod2nix/pull/24
url = "github:doronbehar/gomod2nix/go-stdenv";
inputs.nixpkgs.follows = "nixpkgs";
inputs.utils.follows = "flake-utils";
};
Expand Down Expand Up @@ -39,6 +40,9 @@
# https://discourse.nixos.org/t/passing-git-commit-hash-and-tag-to-build-with-flakes/11355/2
version_rev = if (self ? rev) then (builtins.substring 0 8 self.rev) else "dirty";
version = "${pkgs.lib.fileContents ./VERSION}-${version_rev}-flake";
buildGoApplicationStatic = pkgs.buildGoApplication.override {
stdenv = pkgs.pkgsStatic.stdenv;
};
pistol = pkgs.buildGoApplication {
pname = "pistol";
inherit version;
Expand Down Expand Up @@ -79,6 +83,38 @@
;
CGO_ENABLED = 1;
};
pistol-static = buildGoApplicationStatic {
inherit (pistol)
pname
version
src
subPackages
postBuild
CGO_ENABLED
modules
meta
;
nativeBuildInputs = pistol.nativeBuildInputs ++ [
pkgs.removeReferencesTo
];
buildInputs = [
pkgs.pkgsStatic.file
pkgs.pkgsStatic.zlib
];
# From some reason even though zlib is static we need this, but it
# doesn't create a real reference to zlib.
NIX_LDFLAGS = "-lz";
postFixup = ''
# Remove unnecessary references to zlib.
rm -r $out/nix-support
# Remove more unnecessary references which I don't know the source of
# which. I guess they are due to features of some go modules I don't
# use.
remove-references-to -t ${pkgs.mailcap} $out/bin/pistol
remove-references-to -t ${pkgs.iana-etc} $out/bin/pistol
remove-references-to -t ${pkgs.tzdata} $out/bin/pistol
'';
};
in rec {
devShell = pkgs.mkShell {
inherit (pistol) buildInputs;
Expand All @@ -88,7 +124,12 @@
pkgs.gomod2nix
];
};
packages.pistol = pistol;
packages = {
inherit
pistol
pistol-static
;
};
defaultPackage = pistol;
apps.pistol = {
type = "app";
Expand Down

0 comments on commit 1cabbce

Please sign in to comment.