Skip to content

Commit

Permalink
Actually use the EMBED_MAGIC_DB tag in the static build
Browse files Browse the repository at this point in the history
Also refactor a bit to make cross compilation easier to write.
  • Loading branch information
doronbehar committed Nov 27, 2021
1 parent 8178bb0 commit b0008d7
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@
# 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 {
# Create the buildGoApplication variants
inherit (pkgs) buildGoApplication;
buildGoApplicationStatic = buildGoApplication.override {
stdenv = pkgs.pkgsStatic.stdenv;
};
pistol = pkgs.buildGoApplication {
# Used also in the devShell
MAGIC_DB = "${pkgs.pkgsStatic.file}/share/misc/magic.mgc";
# arguments used in many derivation arguments calls
common-drv-args = {
pname = "pistol";
inherit version;
src = pkgs.lib.cleanSourceWith {
Expand Down Expand Up @@ -76,38 +81,25 @@
modules = ./gomod2nix.toml;
inherit (pkgs.pistol)
nativeBuildInputs
buildInputs
subPackages
postBuild
meta
;
CGO_ENABLED = 1;
};
MAGIC_DB = "${pkgs.pkgsStatic.file}/share/misc/magic.mgc";
pistol-static = buildGoApplicationStatic {
inherit (pistol)
pname
version
src
subPackages
postBuild
CGO_ENABLED
modules
meta
;
nativeBuildInputs = pistol.nativeBuildInputs ++ [
common-static-drv-args = (common-drv-args // {
nativeBuildInputs = common-drv-args.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";
preBuild = ''
cp ${MAGIC_DB} ./cmd/pistol/magic.mgc
'';
buildFlags = ''
-tags EMBED_MAGIC_DB
'';
postFixup = ''
# Remove unnecessary references to zlib.
rm -r $out/nix-support
Expand All @@ -118,7 +110,19 @@
remove-references-to -t ${pkgs.iana-etc} $out/bin/pistol
remove-references-to -t ${pkgs.tzdata} $out/bin/pistol
'';
};
});
pistol = buildGoApplication (common-drv-args // {
inherit (pkgs.pistol) buildInputs;
});
pistol-static = buildGoApplicationStatic (common-static-drv-args // {
buildInputs = [
pkgs.pkgsStatic.file
pkgs.pkgsStatic.zlib
];
postFixup = common-static-drv-args.postFixup + ''
remove-references-to -t ${pkgs.pkgsStatic.file} $out/bin/pistol
'';
});
in rec {
devShell = pkgs.mkShell {
inherit (pistol) buildInputs;
Expand Down

0 comments on commit b0008d7

Please sign in to comment.