Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pistol-static compilation #72

Merged
merged 2 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
/pistol
result
pistol.1
cmd/pistol/magic.mgc
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

NAME := pistol
VERSION := v$(shell cat VERSION)-git
ifdef MAGIC_DB
MAGIC_DB := $(MAGIC_DB)
else
MAGIC_DB := /usr/share/misc/magic.mgc
endif

build:
go build -ldflags "-X 'main.Version=$(VERSION)'" ./cmd/pistol
build-static:
@echo copying magic db for compilation from:
@echo " $(MAGIC_DB)"
@cp --no-preserve=mode,ownership -f $(MAGIC_DB) ./cmd/pistol/magic.mgc
go build -tags EMBED_MAGIC_DB -ldflags "-X 'main.Version=$(VERSION)'" ./cmd/pistol

# Manpage
pistol.1: README.adoc
Expand Down
8 changes: 7 additions & 1 deletion cmd/pistol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/doronbehar/pistol"
"github.com/doronbehar/magicmime"
"github.com/alexflint/go-arg"
"github.com/adrg/xdg"
)
Expand Down Expand Up @@ -45,7 +46,12 @@ func main() {
log.Fatalf("no arguments!")
os.Exit(1)
}
previewer, err := pistol.NewPreviewer(args.FilePath, args.Config, args.Extras)
magic_db_path, err := GetDbPath(magicmime.Version())
if err != nil {
log.Fatal(err)
os.Exit(2)
}
previewer, err := pistol.NewPreviewer(magic_db_path, args.FilePath, args.Config, args.Extras)
if err != nil {
log.Fatal(err)
os.Exit(2)
Expand Down
8 changes: 8 additions & 0 deletions cmd/pistol/previewer_nostatic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//+build !EMBED_MAGIC_DB

package main

func GetDbPath(magicmime_version int) (string, error) {
// And by that use the default location for the magic.mgc database
return "", nil
}
50 changes: 50 additions & 0 deletions cmd/pistol/previewer_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//+build EMBED_MAGIC_DB

package main

import (
_ "embed"
"os"
"fmt"
"errors"
"path/filepath"

"github.com/adrg/xdg"
)

//go:embed magic.mgc
var magic_db_data []byte

func GetDbPath(magicmime_version int) (string, error) {
// Create a copy of the magic database file with magic_db_data
magic_db_dir := fmt.Sprintf("%s/pistol", xdg.DataHome)
err := os.MkdirAll(magic_db_dir, 0755)
if err != nil {
return "", errors.New(fmt.Sprintf(
"We've had issues creating a directory for the libmagic database at %s, error is: %s",
magic_db_dir, err,
))
}
magic_db_path := fmt.Sprintf("%s/%d.mgc", magic_db_dir, magicmime_version)
// Don't write the database if there's already a file there.
if _, err := os.Stat(magic_db_path); errors.Is(err, os.ErrNotExist) {
old_dbs, err := filepath.Glob(filepath.Join(magic_db_dir, "*.mgc"))
if err != nil {
return "", err
}
for _, old_db := range old_dbs {
err = os.Remove(old_db)
if err != nil {
return "", err
}
}
err = os.WriteFile(magic_db_path, magic_db_data, 0644)
if err != nil {
return "", errors.New(fmt.Sprintf(
"Could not create a copy of libmagic database at %s, error is: %s",
magic_db_path, err,
))
}
}
return magic_db_path, nil
}
29 changes: 15 additions & 14 deletions flake.lock

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

50 changes: 48 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,42 @@
;
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 ++ [
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
'';
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 @@ -87,8 +127,14 @@
pkgs.elinks
pkgs.gomod2nix
];
inherit MAGIC_DB;
};
packages = {
inherit
pistol
pistol-static
;
};
packages.pistol = pistol;
defaultPackage = pistol;
apps.pistol = {
type = "app";
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module github.com/doronbehar/pistol

go 1.12
go 1.16

require (
github.com/adrg/xdg v0.4.0
github.com/alecthomas/chroma v0.9.4
github.com/alessio/shellescape v1.4.1
github.com/alexflint/go-arg v1.4.2
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/doronbehar/magicmime v0.1.1-0.20211127135329-3de4ff29dc49
github.com/dustin/go-humanize v1.0.0
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/mholt/archiver/v3 v3.5.1
github.com/nwaples/rardecode v1.1.2
github.com/pierrec/lz4/v4 v4.1.10 // indirect
github.com/rakyll/magicmime v0.1.0
github.com/pierrec/lz4/v4 v4.1.11 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/ulikunitz/xz v0.5.10 // indirect
golang.org/x/sys v0.0.0-20211105183446-c75c47738b0c // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
)
17 changes: 8 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxj
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/doronbehar/magicmime v0.1.1-0.20211127135329-3de4ff29dc49 h1:3f9QFV/yQR4UD8Gv3UE2O6bDAABbBk5+3Rrh5k3qSAo=
github.com/doronbehar/magicmime v0.1.1-0.20211127135329-3de4ff29dc49/go.mod h1:elWi9xask6j0RTycB89gmgcb2k/jaOfyFctNqVLK68M=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
Expand All @@ -39,12 +41,10 @@ github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWk
github.com/nwaples/rardecode v1.1.2 h1:Cj0yZY6T1Zx1R7AhTbyGSALm44/Mmq+BAPc4B/p/d3M=
github.com/nwaples/rardecode v1.1.2/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pierrec/lz4/v4 v4.1.10 h1:H0LgOg/8kTVhiN8ESXFa+gvt9udNp1hQ+mYNDdcMPTM=
github.com/pierrec/lz4/v4 v4.1.10/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pierrec/lz4/v4 v4.1.11 h1:LVs17FAZJFOjgmJXl9Tf13WfLUvZq7/RjfEJrnwZ9OE=
github.com/pierrec/lz4/v4 v4.1.11/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/magicmime v0.1.0 h1:aFIp1DqgzjcB3FI7rQk6uZl73i1VPpWswab1YKU4CL4=
github.com/rakyll/magicmime v0.1.0/go.mod h1:OKs4S+1GpIAB1PCebhwp3rxhyipe7TiImiIeVyFlQt8=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -59,10 +59,9 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofm
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211105183446-c75c47738b0c h1:+8miTPjMCTXwih7BQmvWwd0PjdBZq2MKp/qQaahSzEM=
golang.org/x/sys v0.0.0-20211105183446-c75c47738b0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading