Skip to content

Commit

Permalink
Add flake.nix (#357)
Browse files Browse the repository at this point in the history
* Add flake.nix

* Add profile `opt`

* Update .gitignore

* Fix Windows binary not being built properly

* Split `flake.nix` into files
  • Loading branch information
tirr-c authored Oct 12, 2024
1 parent 6d2d401 commit 1dea00c
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/result
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ version = "0.1.40"
default-features = false
features = ["std"]

[profile.release]
[profile.opt]
inherits = "release"
debug = 1
89 changes: 89 additions & 0 deletions build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
let
inherit (builtins) fetchTarball fromJSON readFile;
getFlake =
name: with (fromJSON (readFile ./flake.lock)).nodes.${name}.locked; {
inherit rev;
outPath = fetchTarball {
url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";
sha256 = narHash;
};
};
in

{
pkgs,
lib ? pkgs.lib,
windows ? pkgs.windows,
hostPlatform ? pkgs.hostPlatform,
stdenv ? pkgs.stdenv,
naersk,

crossTarget ? null,
static ? false,
enableMimalloc ? true,
enableRayon ? true,
enableFfmpeg ? false,
enableDevtools ? enableFfmpeg,
...
}:

let
inherit (builtins)
concatStringsSep
replaceStrings
isNull
;

jxlOxideCliToml = builtins.fromTOML (builtins.readFile ./crates/jxl-oxide-cli/Cargo.toml);

toScreamingSnakeCase = s: replaceStrings [ "-" ] [ "_" ] (lib.strings.toUpper s);
cargoEnvPrefix = "CARGO_TARGET_${toScreamingSnakeCase crossTarget}_";

commonBuildArgs = {
inherit (jxlOxideCliToml.package) name version;
pname = "jxl-oxide";
src = ./.;
strictDeps = true;
overrideMain = old: {
preConfigure = ''
cargo_build_options="$cargo_build_options -p jxl-oxide-cli"
'';
};
};

cargoBuildArgs =
let
featureList =
[ ]
++ lib.optional enableMimalloc "mimalloc"
++ lib.optional enableRayon "rayon"
++ lib.optional enableDevtools "__devtools"
++ lib.optional enableFfmpeg "__ffmpeg";
featureListStr = concatStringsSep "," featureList;
in
[ "--no-default-features" ]
++ lib.optionals (featureList != [ ]) [
"--features"
featureListStr
];

isMinGW = stdenv.cc.isGNU or false && hostPlatform.isWindows;
in
naersk.buildPackage (
commonBuildArgs
// {
cargoBuildOptions = args: args ++ cargoBuildArgs;
}
// lib.optionalAttrs (!isNull crossTarget) rec {
depsBuildBuild = [
stdenv.cc
] ++ lib.optional isMinGW windows.pthreads;

CARGO_BUILD_TARGET = crossTarget;
TARGET_CC = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
"${cargoEnvPrefix}LINKER" = TARGET_CC;
}
// lib.optionalAttrs static {
"${cargoEnvPrefix}RUSTFLAGS" = "-C target-feature=+crt-static";
}
)
26 changes: 26 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
let
inherit (builtins) fetchTarball fromJSON readFile;
getFlake =
name: with (fromJSON (readFile ./flake.lock)).nodes.${name}.locked; {
inherit rev;
outPath = fetchTarball {
url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";
sha256 = narHash;
};
};

pkgs = import <nixpkgs> { };
fenix = pkgs.callPackage (getFlake "fenix") { };
toolchain = fenix.stable.withComponents [
"cargo"
"rustc"
];
naersk = pkgs.callPackage (getFlake "naersk") {
cargo = toolchain;
rustc = toolchain;
};
in

pkgs.callPackage ./build.nix {
inherit naersk;
}
120 changes: 120 additions & 0 deletions flake.lock

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

Loading

0 comments on commit 1dea00c

Please sign in to comment.