mirror of
https://github.com/nix-community/naersk
synced 2024-11-13 23:47:08 +00:00
Split config out of default
This commit is contained in:
parent
737e66e399
commit
1e4b63a406
2 changed files with 111 additions and 109 deletions
95
config.nix
Normal file
95
config.nix
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
{ lib, libb, builtinz, src, attrs }:
|
||||||
|
rec
|
||||||
|
{ usePureFromTOML = attrs.usePureFromTOML or true;
|
||||||
|
readTOML = builtinz.readTOML usePureFromTOML;
|
||||||
|
|
||||||
|
compressTarget = attrs.compressTarget or true;
|
||||||
|
|
||||||
|
# Whether we skip pre-building the deps
|
||||||
|
isSingleStep = attrs.singleStep or false;
|
||||||
|
|
||||||
|
# The members we want to build
|
||||||
|
# (list of directory names)
|
||||||
|
wantedMembers =
|
||||||
|
lib.mapAttrsToList (member: _cargotoml: member) wantedMemberCargotomls;
|
||||||
|
|
||||||
|
# Member path to cargotoml
|
||||||
|
# (attrset from directory name to Nix object)
|
||||||
|
wantedMemberCargotomls =
|
||||||
|
let pred =
|
||||||
|
if ! isWorkspace
|
||||||
|
then (_member: _cargotoml: true)
|
||||||
|
else
|
||||||
|
if builtins.hasAttr "targets" attrs
|
||||||
|
then (_member: cargotoml: lib.elem cargotoml.package.name attrs.targets)
|
||||||
|
else (member: _cargotoml: member != "."); in
|
||||||
|
lib.filterAttrs pred cargotomls;
|
||||||
|
|
||||||
|
# All cargotomls, from path to nix object
|
||||||
|
# (attrset from directory name to Nix object)
|
||||||
|
cargotomls =
|
||||||
|
let readTOML = builtinz.readTOML usePureFromTOML; in
|
||||||
|
|
||||||
|
{ "." = toplevelCargotoml; } //
|
||||||
|
lib.optionalAttrs isWorkspace
|
||||||
|
(lib.listToAttrs
|
||||||
|
(map
|
||||||
|
(member:
|
||||||
|
{ name = member;
|
||||||
|
value = readTOML (src + "/${member}/Cargo.toml");
|
||||||
|
}
|
||||||
|
)
|
||||||
|
(toplevelCargotoml.workspace.members or [])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
patchedSources =
|
||||||
|
let
|
||||||
|
mkRelative = po:
|
||||||
|
if lib.hasPrefix "/" po.path
|
||||||
|
then throw "'${toString src}/Cargo.toml' contains the absolute path '${toString po.path}' which is not allowed under a [patch] section by naersk. Please make it relative to '${toString src}'"
|
||||||
|
else src + "/" + po.path;
|
||||||
|
in lib.optionals (builtins.hasAttr "patch" toplevelCargotoml)
|
||||||
|
(map mkRelative
|
||||||
|
(lib.collect (as: lib.isAttrs as && builtins.hasAttr "path" as)
|
||||||
|
toplevelCargotoml.patch));
|
||||||
|
|
||||||
|
# Are we building a workspace (or is this a simple crate) ?
|
||||||
|
isWorkspace = builtins.hasAttr "workspace" toplevelCargotoml;
|
||||||
|
|
||||||
|
# The top level Cargo.toml, either a workspace or package
|
||||||
|
toplevelCargotoml = readTOML (src + "/Cargo.toml");
|
||||||
|
|
||||||
|
# The cargo lock
|
||||||
|
cargolock = readTOML (src + "/Cargo.lock");
|
||||||
|
|
||||||
|
# The list of paths to Cargo.tomls. If this is a workspace, the paths
|
||||||
|
# are the members. Otherwise, there is a single path, ".".
|
||||||
|
cratePaths = lib.concatStringsSep "\n" wantedMembers;
|
||||||
|
|
||||||
|
packageName = attrs.name or toplevelCargotoml.package.name or
|
||||||
|
(if isWorkspace then "rust-workspace" else "rust-package");
|
||||||
|
|
||||||
|
packageVersion = attrs.version or toplevelCargotoml.package.version or
|
||||||
|
"unknown";
|
||||||
|
|
||||||
|
# The list of _all_ crates (incl. transitive dependencies) with name,
|
||||||
|
# version and sha256 of the crate
|
||||||
|
# Example:
|
||||||
|
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
|
||||||
|
crateDependencies = libb.mkVersions cargolock;
|
||||||
|
|
||||||
|
preBuild = ''
|
||||||
|
# Cargo uses mtime, and we write `src/lib.rs` and `src/main.rs`in
|
||||||
|
# the dep build step, so make sure cargo rebuilds stuff
|
||||||
|
if [ -f src/lib.rs ] ; then touch src/lib.rs; fi
|
||||||
|
if [ -f src/main.rs ] ; then touch src/main.rs; fi
|
||||||
|
'';
|
||||||
|
|
||||||
|
cargoBuild = attrs.cargoBuild or ''
|
||||||
|
cargo build "''${cargo_release}" -j $NIX_BUILD_CORES -Z unstable-options --out-dir out
|
||||||
|
'';
|
||||||
|
cargoTestCommands = attrs.cargoTestCommands or [
|
||||||
|
''cargo test "''${cargo_release}" -j $NIX_BUILD_CORES''
|
||||||
|
];
|
||||||
|
}
|
125
default.nix
125
default.nix
|
@ -41,111 +41,18 @@ let
|
||||||
# Crate building
|
# Crate building
|
||||||
with rec
|
with rec
|
||||||
{
|
{
|
||||||
commonAttrs = src: attrs: rec
|
mkConfig = src: attrs:
|
||||||
{ usePureFromTOML = attrs.usePureFromTOML or true;
|
import ./config.nix { inherit lib src attrs libb builtinz; };
|
||||||
readTOML = builtinz.readTOML usePureFromTOML;
|
|
||||||
|
|
||||||
compressTarget = attrs.compressTarget or true;
|
|
||||||
|
|
||||||
# Whether we skip pre-building the deps
|
|
||||||
isSingleStep = attrs.singleStep or false;
|
|
||||||
|
|
||||||
# The members we want to build
|
|
||||||
# (list of directory names)
|
|
||||||
wantedMembers =
|
|
||||||
lib.mapAttrsToList (member: _cargotoml: member) wantedMemberCargotomls;
|
|
||||||
|
|
||||||
# Member path to cargotoml
|
|
||||||
# (attrset from directory name to Nix object)
|
|
||||||
wantedMemberCargotomls =
|
|
||||||
let pred =
|
|
||||||
if ! isWorkspace
|
|
||||||
then (_member: _cargotoml: true)
|
|
||||||
else
|
|
||||||
if builtins.hasAttr "targets" attrs
|
|
||||||
then (_member: cargotoml: lib.elem cargotoml.package.name attrs.targets)
|
|
||||||
else (member: _cargotoml: member != "."); in
|
|
||||||
lib.filterAttrs pred cargotomls;
|
|
||||||
|
|
||||||
# All cargotomls, from path to nix object
|
|
||||||
# (attrset from directory name to Nix object)
|
|
||||||
cargotomls =
|
|
||||||
let readTOML = builtinz.readTOML usePureFromTOML; in
|
|
||||||
|
|
||||||
{ "." = toplevelCargotoml; } //
|
|
||||||
lib.optionalAttrs isWorkspace
|
|
||||||
(lib.listToAttrs
|
|
||||||
(map
|
|
||||||
(member:
|
|
||||||
{ name = member;
|
|
||||||
value = readTOML (src + "/${member}/Cargo.toml");
|
|
||||||
}
|
|
||||||
)
|
|
||||||
(toplevelCargotoml.workspace.members or [])
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
patchedSources =
|
|
||||||
let
|
|
||||||
mkRelative = po:
|
|
||||||
if lib.hasPrefix "/" po.path
|
|
||||||
then throw "'${toString src}/Cargo.toml' contains the abolsute path '${toString po.path}' which is not allowed under a [patch] section by naersk. Please make it relative to '${toString src}'"
|
|
||||||
else src + "/" + po.path;
|
|
||||||
in lib.optionals (builtins.hasAttr "patch" toplevelCargotoml)
|
|
||||||
(map mkRelative
|
|
||||||
(lib.collect (as: lib.isAttrs as && builtins.hasAttr "path" as)
|
|
||||||
toplevelCargotoml.patch));
|
|
||||||
|
|
||||||
# Are we building a workspace (or is this a simple crate) ?
|
|
||||||
isWorkspace = builtins.hasAttr "workspace" toplevelCargotoml;
|
|
||||||
|
|
||||||
# The top level Cargo.toml, either a workspace or package
|
|
||||||
toplevelCargotoml = readTOML (src + "/Cargo.toml");
|
|
||||||
|
|
||||||
# The cargo lock
|
|
||||||
cargolock = readTOML (src + "/Cargo.lock");
|
|
||||||
|
|
||||||
# The list of paths to Cargo.tomls. If this is a workspace, the paths
|
|
||||||
# are the members. Otherwise, there is a single path, ".".
|
|
||||||
cratePaths = lib.concatStringsSep "\n" wantedMembers;
|
|
||||||
|
|
||||||
packageName = attrs.name or toplevelCargotoml.package.name or
|
|
||||||
(if isWorkspace then "rust-workspace" else "rust-package");
|
|
||||||
|
|
||||||
packageVersion = attrs.version or toplevelCargotoml.package.version or
|
|
||||||
"unknown";
|
|
||||||
|
|
||||||
# The list of _all_ crates (incl. transitive dependencies) with name,
|
|
||||||
# version and sha256 of the crate
|
|
||||||
# Example:
|
|
||||||
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
|
|
||||||
crateDependencies = libb.mkVersions cargolock;
|
|
||||||
|
|
||||||
preBuild = ''
|
|
||||||
# Cargo uses mtime, and we write `src/lib.rs` and `src/main.rs`in
|
|
||||||
# the dep build step, so make sure cargo rebuilds stuff
|
|
||||||
if [ -f src/lib.rs ] ; then touch src/lib.rs; fi
|
|
||||||
if [ -f src/main.rs ] ; then touch src/main.rs; fi
|
|
||||||
'';
|
|
||||||
|
|
||||||
cargoBuild = attrs.cargoBuild or ''
|
|
||||||
cargo build "''${cargo_release}" -j $NIX_BUILD_CORES -Z unstable-options --out-dir out
|
|
||||||
'';
|
|
||||||
cargoTestCommands = attrs.cargoTestCommands or [
|
|
||||||
''cargo test "''${cargo_release}" -j $NIX_BUILD_CORES''
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
buildPackage = src: attrs:
|
buildPackage = src: attrs:
|
||||||
with (commonAttrs src attrs);
|
let config = (mkConfig src attrs); in
|
||||||
import ./build.nix src
|
import ./build.nix src
|
||||||
(defaultBuildAttrs //
|
(defaultBuildAttrs //
|
||||||
{ pname = packageName;
|
{ pname = config.packageName;
|
||||||
version = packageVersion;
|
version = config.packageVersion;
|
||||||
inherit cratePaths crateDependencies preBuild cargoBuild cargoTestCommands compressTarget;
|
inherit (config) cratePaths crateDependencies preBuild cargoBuild cargoTestCommands compressTarget;
|
||||||
} //
|
} //
|
||||||
(removeAttrs attrs [ "targets" "usePureFromTOML" "cargotomls" "singleStep" ]) //
|
(removeAttrs attrs [ "usePureFromTOML" "cargotomls" "singleStep" ]) //
|
||||||
{ builtDependencies = lib.optional (! isSingleStep)
|
{ builtDependencies = lib.optional (! config.isSingleStep)
|
||||||
(
|
(
|
||||||
import ./build.nix
|
import ./build.nix
|
||||||
(libb.dummySrc
|
(libb.dummySrc
|
||||||
|
@ -153,19 +60,19 @@ with rec
|
||||||
if builtinz.pathExists (toString src + "/.cargo/config")
|
if builtinz.pathExists (toString src + "/.cargo/config")
|
||||||
then builtins.readFile (src + "/.cargo/config")
|
then builtins.readFile (src + "/.cargo/config")
|
||||||
else null;
|
else null;
|
||||||
cargolock = cargolock;
|
cargolock = config.cargolock;
|
||||||
cargotomls = cargotomls;
|
cargotomls = config.cargotomls;
|
||||||
inherit patchedSources;
|
inherit (config) patchedSources;
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
(defaultBuildAttrs //
|
(defaultBuildAttrs //
|
||||||
{ pname = "${packageName}-deps";
|
{ pname = "${config.packageName}-deps";
|
||||||
version = packageVersion;
|
version = config.packageVersion;
|
||||||
inherit cratePaths crateDependencies cargoBuild compressTarget;
|
inherit (config) cratePaths crateDependencies cargoBuild compressTarget;
|
||||||
} //
|
} //
|
||||||
(removeAttrs attrs [ "targets" "usePureFromTOML" "cargotomls" "singleStep"]) //
|
(removeAttrs attrs [ "usePureFromTOML" "cargotomls" "singleStep"]) //
|
||||||
{ preBuild = "";
|
{ preBuild = "";
|
||||||
cargoTestCommands = map (cmd: "${cmd} || true") cargoTestCommands;
|
cargoTestCommands = map (cmd: "${cmd} || true") config.cargoTestCommands;
|
||||||
copyTarget = true;
|
copyTarget = true;
|
||||||
copyBins = false;
|
copyBins = false;
|
||||||
copyDocsToSeparateOutput = false;
|
copyDocsToSeparateOutput = false;
|
||||||
|
|
Loading…
Reference in a new issue