Last active
May 27, 2023 04:26
-
-
Save alekseysidorov/5e027e7574cd47d720324f00c20eeb8e to your computer and use it in GitHub Desktop.
Revisions
-
alekseysidorov revised this gist
Mar 11, 2023 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,6 @@ pkgs.mkShell { docker ]; text = let shellFile = ./shell.nix; in '' binary_name=$1 # Compile cargo binary cargo build --release -
alekseysidorov revised this gist
Mar 11, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -66,7 +66,7 @@ pkgs.mkShell { Usage: ```shell cargo-nix-docker-image executable-name> ``` */ passthru.dockerImage = ( -
alekseysidorov created this gist
Mar 11, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,104 @@ { localSystem ? builtins.currentSystem , crossSystem ? { config = "x86_64-unknown-linux-musl"; isStatic = true; useLLVM = true; } }: let # Fetch the nixpkgs-cross-overlay sources. src = builtins.fetchTarball "https://github.com/alekseysidorov/nixpkgs-cross-overlay/tarball/main"; # Use the nixpkgs revision provided by the overlay. # This is the best way, as they are the most proven and compatible. nixpkgs = "${src}/utils/nixpkgs.nix"; # Make cross system packages. pkgs = import nixpkgs { inherit localSystem crossSystem; overlays = [ # <- You may add your extra overlays here. ]; }; in # And now, with the resulting packages, we can describe the cross-compilation shell. pkgs.mkShell { # Native project dependencies like build utilities and additional routines # like container building, linters, etc. nativeBuildInputs = [ pkgs.pkgsBuildHost.rust-bin.stable.latest.default # Will add some dependencies like libiconv. pkgs.pkgsBuildHost.rustBuildHostDependencies # Crates dependencies. pkgs.cargoDeps.openssl-sys # A simple script to create a docker image from the Cargo workspace member. (pkgs.pkgsBuildHost.writeShellApplication { name = "cargo-nix-docker-image"; runtimeInputs = with pkgs.pkgsBuildHost; [ nix docker ]; text = let shellFile = ./shell.nix; in '' set -x binary_name=$1 # Compile cargo binary cargo build --release # Copy this shell to the target dir cp ${shellFile} ./target/shell.nix # Build docker image from the compiled service image_archive=$(nix-build ./target/shell.nix -A dockerImage --argstr name "$binary_name") docker load <"$image_archive" ''; }) ]; # Libraries essential to build the service binaries. buildInputs = with pkgs; [ # Enable Rust cross-compilation support. rustCrossHook ]; # Prettify shell prompt. shellHook = '' ${pkgs.crossBashPrompt} echo "Welcome to the Cargo docker images builder demo shell!" echo "" echo "Usage:" echo "" echo "$ cargo-nix-docker-image <executable-name>" echo "" echo "Have a nice day!" ''; /* Service docker image definition Usage: ```shell cargo-nix-docker-image <workspace-member> ``` */ passthru.dockerImage = ( { # Cargo workspace member name name , tag ? "latest" }: pkgs.pkgsBuildHost.dockerTools.buildLayeredImage { inherit tag name; contents = with pkgs; [ coreutils bashInteractive dockerTools.caCertificates # Actual service binary compiled by Cargo (copyBinaryFromCargoBuild { inherit name; targetDir = ./.; buildInputs = [ openssl.dev ]; }) # Utilites like ldd to help image debugging stdenv.cc.libc_bin ]; config = { Cmd = [ name ]; WorkingDir = "/"; Expose = 8080; }; } ); }