Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support incremental builds with CMake #288

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,31 @@ naersk.buildPackage {
}
```

### Using CMake

If your application uses CMake, the build process might fail, saying:

```
CMake Error: The current CMakeCache.txt directory ... is different than the directory ... where CMakeCache.txt was created.
```

You can fix this problem by removing stale `CMakeCache.txt` files before the
build:

``` nix
naersk.buildPackage {
# ...

preBuild = ''
find \
-name CMakeCache.txt \
-exec rm {} \;
'';
}
```

([context](https://github.com/nix-community/naersk/pull/288))

### Using OpenSSL

If your application uses OpenSSL (making the build process fail), try:
Expand Down
25 changes: 25 additions & 0 deletions README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,31 @@ naersk.buildPackage {
}
```

### Using CMake

If your application uses CMake, the build process might fail, saying:

```
CMake Error: The current CMakeCache.txt directory ... is different than the directory ... where CMakeCache.txt was created.
```

You can fix this problem by removing stale `CMakeCache.txt` files before the
build:

``` nix
naersk.buildPackage {
# ...

preBuild = ''
find \
-name CMakeCache.txt \
-exec rm {} \;
'';
}
```

([context](https://github.com/nix-community/naersk/pull/288))

### Using OpenSSL

If your application uses OpenSSL (making the build process fail), try:
Expand Down
12 changes: 12 additions & 0 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"url": "https://github.com/ninegua/agent-rs/archive/4a22e590516bc79ec3c75a320f7941e7762ea098.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"fenix": {
Patryk27 marked this conversation as resolved.
Show resolved Hide resolved
"branch": "main",
"description": "Rust toolchains and rust-analyzer nightly for Nix [maintainer=@figsoda]",
"homepage": "",
"owner": "nix-community",
"repo": "fenix",
"rev": "06776a448cfdee8ed146305369c1810cd7c82799",
"sha256": "15z2mcrgzj9322xndk1xs29m23rdkfzbg2b0w0hva489rnq4mnc3",
"type": "tarball",
"url": "https://github.com/nix-community/fenix/archive/06776a448cfdee8ed146305369c1810cd7c82799.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"lorri": {
"branch": "master",
"description": "foo",
Expand Down
118 changes: 71 additions & 47 deletions nix/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,50 @@ let
let
Patryk27 marked this conversation as resolved.
Show resolved Hide resolved
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };

fetch_tarball = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };

fetch_git = name: spec:
let
ref =
if spec ? ref then spec.ref else
spec.ref or (
if spec ? branch then "refs/heads/${spec.branch}" else
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"
);
submodules = spec.submodules or false;
submoduleArg =
let
nixSupportsSubmodules = builtins.compareVersions builtins.nixVersion "2.4" >= 0;
emptyArgWithWarning =
if submodules
then
builtins.trace
(
"The niv input \"${name}\" uses submodules "
+ "but your nix's (${builtins.nixVersion}) builtins.fetchGit "
+ "does not support them"
)
{ }
else { };
in
if nixSupportsSubmodules
then { inherit submodules; }
else emptyArgWithWarning;
in
builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
builtins.fetchGit
({ url = spec.repo; inherit (spec) rev; inherit ref; } // submoduleArg);

fetch_local = spec: spec.path;

Expand Down Expand Up @@ -66,16 +87,16 @@ let
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> {}
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> { }
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';

# The actual fetching function.
fetch = pkgs: name: spec:
Expand All @@ -95,13 +116,13 @@ let
# the path directly as opposed to the fetched source.
replace = name: drv:
let
saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
in
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";

# Ports of functions for older nix versions

Expand All @@ -112,7 +133,7 @@ let
);

# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);
range = first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);

# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
Expand All @@ -123,43 +144,46 @@ let
concatStrings = builtins.concatStringsSep "";

# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else {};
optionalAttrs = cond: as: if cond then as else { };

# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchTarball attrs;
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
else
fetchTarball attrs;

# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchurl attrs;
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
else
fetchurl attrs;

# Create the final "sources" from the config
mkSources = config:
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
) config.sources;
mapAttrs
(
name: spec:
if builtins.hasAttr "outPath" spec
then
abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
)
config.sources;

# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
, sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
, sources ? if sourcesFile == null then { } else builtins.fromJSON (builtins.readFile sourcesFile)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
}: rec {
Expand All @@ -171,4 +195,4 @@ let
};

in
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }
mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); }
1 change: 0 additions & 1 deletion test/slow/agent-rs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

naersk.buildPackage {
src = sources.agent-rs;
doCheck = true;
Patryk27 marked this conversation as resolved.
Show resolved Hide resolved

buildInputs = [
pkgs.openssl
Expand Down
21 changes: 11 additions & 10 deletions test/slow/nushell/default.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{ sources, ... }:
{ sources, pkgs, ... }:
let
pkgs = import sources.nixpkgs {
overlays = [
(import sources.nixpkgs-mozilla)
];
};
fenix = import sources.fenix { };

toolchain = (pkgs.rustChannelOf {
rustToolchain = "${sources.nushell}/rust-toolchain.toml";
toolchain = fenix.fromToolchainFile {
file = "${sources.nushell}/rust-toolchain.toml";
sha256 = "sha256-Zk2rxv6vwKFkTTidgjPm6gDsseVmmljVt201H7zuDkk=";
}).rust;
};

naersk = pkgs.callPackage ../../../default.nix {
cargo = toolchain;
Expand All @@ -18,8 +14,13 @@ let

app = naersk.buildPackage {
src = sources.nushell;

nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = with pkgs; [ openssl ];

buildInputs = with pkgs; [ openssl ]
++ lib.optionals stdenv.isDarwin [ zlib libiconv darwin.Libsystem darwin.Security darwin.apple_sdk.frameworks.Foundation ];

LIBCLANG_PATH = "${pkgs.clang.cc.lib}/lib";
};

in
Expand Down