Skip to content

Instantly share code, notes, and snippets.

@thalesmg
Created February 9, 2020 10:26
Show Gist options
  • Save thalesmg/fb04f91e789db460485db33c795d8c7c to your computer and use it in GitHub Desktop.
Save thalesmg/fb04f91e789db460485db33c795d8c7c to your computer and use it in GitHub Desktop.
How to statically compile a Rust binary using MUSL
cargo new rust-static-nix
cd rust-static-nix
cargo build
# create/copy default.nix
nix-build
let
base-nixpkgs = import <nixpkgs> {};
mozillaOverlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
nixpkgs = import <nixpkgs> { overlays = [ mozillaOverlay ]; };
rust = (nixpkgs.rustChannelOf { channel = "stable"; }).rust.override {
targets = [ "x86_64-unknown-linux-musl" ];
};
rustPlatform = nixpkgs.makeRustPlatform {
cargo = rust;
rustc = rust;
};
in
rustPlatform.buildRustPackage {
name = "rust-static-nix";
src = ./.;
cargoSha256 = "0jacm96l1gw9nxwavqi1x4669cg6lzy9hr18zjpwlcyb3qkw9z7f";
target = "x86_64-unknown-linux-musl";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment