PkgForge
GithubSoar
  • Orgs
    • PkgForge (Core)
      • About
      • Projects
        • Soarpkgs
        • bincache
        • pkgcache
      • People
    • PkgForge-Community
    • PkgForge-Dev
      • Projects
        • Anylinux-AppImages
      • People
    • PkgForge-Security
      • Projects
        • CertStream
  • Soar
    • Soar (Docs)
    • Comparisons
      • 1. Candidates
      • 2. Dependencies
      • 3. Packages
      • 4. Security
  • Formats
    • Binaries
      • Dynamic
      • Static
        • Build Notes
          • Cargo (Rust)
          • GoLang
          • Make
          • Nim
          • Nix
          • Vlang
          • Zig (Musl)
        • Build Tests
    • Packages
      • Archive
      • AppBundle
      • AppImage
      • FlatImage
      • GameImage (TBD)
      • NixAppImage
      • RunImage
      • Errors & Quirks
        • Fonts
        • Fuse
        • Namespaces
        • Zsync
  • SBUILD
    • Introduction
    • Specification
      • 0.Prerequisite
      • 1.Shebang
      • 2.Pkg
      • 3.Version
      • 4.AppID
      • 5.BuildAsset
      • 6.BuildUtil
      • 7.Category
      • 8.Description
      • 9.Desktop
      • 10.DistroPkg
      • 11.HomePage
      • 12.Icon
      • 13.License
      • 14.Maintainer
      • 15.Note
      • 16.Provides
      • 17.Repology
      • 18.SourceURL
      • 19.Tag
      • 20.x_exec
    • Instructions
      • ENV_VARS (x_exec.run)
      • ENV_VARS (SBUILDER)
      • NEEDED_FILES
      • Examples
  • Repositories
    • soarpkgs
      • Contribution
      • Copy of DMCA | Copyright (Cease & Desist)
      • Differences
      • FAQ
      • Infra
      • Metadata
      • Package-Request
      • Security
    • bincache
      • Cache
      • Contribution
      • Differences
      • DMCA | Copyright (Cease & Desist)
      • FAQ
      • Infra
      • Metadata
      • Package-Request
      • Security
    • pkgcache
      • Cache
      • Contribution
      • Differences
      • DMCA | Copyright (Cease & Desist)
      • FAQ
      • Infra
      • Metadata
      • Package-Request
      • Security
    • external
      • AM
      • cargo-bins
      • appimage.github.io
      • AppImageHub
    • Nests
  • Contact
    • Chat
Powered by GitBook
On this page
  • Native
  • Cross
  • Old Versions

Was this helpful?

  1. Formats
  2. Binaries
  3. Static
  4. Build Notes

Nix

https://kokada.dev/blog/building-static-binaries-in-nix/

Native

#Create Dir for later
mkdir -pv "./OUTDIR"

#Build and Output to ./TMPDIR (Symlink)
NIXPKGS_ALLOW_BROKEN="1" NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM="1" \
nix-build '<nixpkgs>' --attr "pkgsStatic.${PKG_NAME}" --impure \
--cores "$(($(nproc)+1))" --max-jobs "$(($(nproc)+1))" \
--log-format bar-with-logs --out-link "./TMPDIR"

#Copy (Dir may differ)
sudo rsync -av --copy-links "./TMPDIR/bin/." "./OUTDIR"
sudo chown -R "$(whoami):$(whoami)" "./OUTDIR" && chmod -R 755 "./OUTDIR"

#Strip & File
find "./OUTDIR" -type f -exec objcopy --remove-section=".comment" --remove-section=".note.*" "{}" \;
find "./OUTDIR" -type f ! -name "*.no_strip" -exec strip --strip-debug --strip-dwo --strip-unneeded --preserve-dates "{}" \;
find "./OUTDIR" -type f -print | xargs -I {} sh -c 'file {}; b3sum {}; sha256sum {}; du -sh {}'

#Cleanup
nix-collect-garbage >/dev/null 2>&1

Cross

#Create Dir for later
mkdir -pv "./OUTDIR"

#${NIX_TARGET}: https://github.com/Azathothas/Toolpacks/blob/main/Docs/NIX_TARGETS.txt
nix-instantiate --eval --strict --expr 'let pkgs = import <nixpkgs> {}; pkgsCross = pkgs.pkgsCross; in builtins.attrNames pkgsCross' | tr -d '[]"' | tr ' ' '\n' | sed '/^\s*$/d'

#Build and Output to ./TMPDIR (Symlink)
NIXPKGS_ALLOW_BROKEN="1" NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM="1" \
nix-build '<nixpkgs>' --attr "pkgsCross.${NIX_TARGET}.pkgsStatic.${PKG_NAME}" --impure \
--cores "$(($(nproc)+1))" --max-jobs "$(($(nproc)+1))" \
--log-format bar-with-logs --out-link "./TMPDIR"

#Copy (Dir may differ)
sudo rsync -av --copy-links "./TMPDIR/bin/." "./OUTDIR"
sudo chown -R "$(whoami):$(whoami)" "./OUTDIR" && chmod -R 755 "./OUTDIR"

#Strip & File
find "./OUTDIR" -type f -exec objcopy --remove-section=".comment" --remove-section=".note.*" "{}" \;
find "./OUTDIR" -type f ! -name "*.no_strip" -exec strip --strip-debug --strip-dwo --strip-unneeded --preserve-dates "{}" \;
find "./OUTDIR" -type f -print | xargs -I {} sh -c 'file {}; b3sum {}; sha256sum {}; du -sh {}'

#Cleanup
nix-collect-garbage >/dev/null 2>&1

Old Versions

!#1. Using GIT_COMMIT_SHA

#Get Version
nix derivation show "github:NixOS/nixpkgs/${COMMIT_SHA}#${PKG_NAME}" --impure --refresh --quiet 2>&1 | jq -r '.. | objects | select(has("version")) | .version'

#Build
nix-build 'https://github.com/NixOS/nixpkgs/archive/${COMMIT_SHA}.tar.gz' --impure --attr "pkgsStatic.${PKG_NAME}" --cores "$(($(nproc)+1))" --max-jobs "$(($(nproc)+1))" --log-format bar-with-logs --out-link "./TMPDIR"

!#2. Using GIT_TAGS: https://github.com/NixOS/nixpkgs/tags

#Get Version
nix derivation show "github:NixOS/nixpkgs/${GIT_TAG}#${PKG_NAME}" --impure --refresh --quiet 2>&1 | jq -r '.. | objects | select(has("version")) | .version'

#Build
nix-build 'https://github.com/NixOS/nixpkgs/archive/refs/tags/${GIT_TAG}.tar.gz' --impure --attr "pkgsStatic.${PKG_NAME}" --cores "$(($(nproc)+1))" --max-jobs "$(($(nproc)+1))" --log-format bar-with-logs --out-link "./TMPDIR"
PreviousNimNextVlang

Last updated 4 months ago

Was this helpful?