Skip to content

Commit

Permalink
Use naersk for nix build
Browse files Browse the repository at this point in the history
  • Loading branch information
cofob committed Jul 28, 2024
1 parent 90bf5df commit 760e2d4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 78 deletions.
49 changes: 15 additions & 34 deletions flake.lock

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

55 changes: 11 additions & 44 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,20 @@

flake-utils.url = "github:numtide/flake-utils";

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
import-cargo.url = "github:edolstra/import-cargo";
naersk.url = "github:nix-community/naersk";
naersk.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, flake-utils, rust-overlay, import-cargo, ... }:
outputs = { self, nixpkgs, flake-utils, naersk, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
rustVersion = pkgs.rust-bin.stable.latest.default.override {
targets = [ "x86_64-unknown-linux-musl" ];
};
inherit (import-cargo.builders) importCargo;

nativeBuildInputs = with pkgs; [ pkg-config rustVersion ];

buildInputs = with pkgs; [ ];

fastside = pkgs.stdenv.mkDerivation {
pname = "fastside";
version = "0.2.0";

src = self;

inherit buildInputs;
pkgs = import nixpkgs { inherit system; };
naersk' = pkgs.callPackage naersk { };

nativeBuildInputs = [
(importCargo {
lockFile = ./Cargo.lock;
inherit pkgs;
}).cargoHome
] ++ nativeBuildInputs;

buildPhase = ''
cargo build --release --offline --target x86_64-unknown-linux-musl
'';

installPhase = ''
install -Dm775 ./target/x86_64-unknown-linux-musl/release/fastside $out/bin/fastside
'';
fastside = naersk'.buildPackage {
src = ./.;
nativeBuildInputs = with pkgs; [ mold ];
NIX_CFLAGS_LINK = " -fuse-ld=mold";
};

fastside-docker = pkgs.dockerTools.buildLayeredImage {
Expand All @@ -63,14 +33,11 @@
default = fastside;
fastside = fastside;
fastside-docker = fastside-docker;
services = pkgs.runCommand "generate-services" {} ''
services = pkgs.runCommand "generate-services" { } ''
cat '${./services.json}' > $out
'';
};

devShells.default = pkgs.mkShell {
buildInputs = (with pkgs; [ nixfmt ]) ++ nativeBuildInputs
++ buildInputs;
};
devShells.default = import ./shell.nix { inherit pkgs; };
});
}
17 changes: 17 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ channel ? "stable", profile ? "default", pkgs ? import <nixpkgs> }:
let
pkgs' = pkgs.extend (import (builtins.fetchTarball {
url =
"https://github.com/oxalica/rust-overlay/archive/9803f6e04ca37a2c072783e8297d2080f8d0e739.tar.gz";
sha256 = "1b566msx04y4s0hvwsza9gcv4djmni4fa6ik7q2m33b6x4vrb92w";
}));
in pkgs'.mkShell {
nativeBuildInputs = with pkgs'; [
nixfmt-classic
# Rust
(if channel == "nightly" then
rust-bin.selectLatestNightlyWith (toolchain: toolchain.${profile})
else
rust-bin.${channel}.latest.${profile})
];
}

0 comments on commit 760e2d4

Please sign in to comment.