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

Use TOML serializer from nixpkgs #300

Merged
merged 5 commits into from
Aug 16, 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
14 changes: 7 additions & 7 deletions .github/workflows/fast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ jobs:
linux:
strategy:
matrix:
nixpkgs: [ nixpkgs, nixpkgs-21.05, nixpkgs-21.11 ]
runs-on: ubuntu-20.04
nixpkgs: [ nixpkgs, nixpkgs-21.05, nixpkgs-22.05, nixpkgs-23.05 ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v16
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
CACHIX_SIGNING_KEY: ${{ secrets.CACHIX_SIGNING_KEY }}
- name: "Run tests"
run: './script/test --fast --nixpkgs "$nixpkgs" '
env:
nixpkgs: ${{ matrix.nixpkgs }}
darwin:
runs-on: macos-10.15
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v16
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
CACHIX_SIGNING_KEY: ${{ secrets.CACHIX_SIGNING_KEY }}
- name: "Run tests"
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ jobs:
linux:
strategy:
matrix:
nixpkgs: [ nixpkgs, nixpkgs-21.05, nixpkgs-21.11 ]
runs-on: ubuntu-20.04
nixpkgs: [ nixpkgs, nixpkgs-21.05, nixpkgs-22.05, nixpkgs-23.05 ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v16
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
CACHIX_SIGNING_KEY: ${{ secrets.CACHIX_SIGNING_KEY }}
- name: "Run tests"
run: './script/test --nixpkgs "$nixpkgs" '
env:
nixpkgs: ${{ matrix.nixpkgs }}
darwin:
runs-on: macos-10.15
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v16
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
CACHIX_SIGNING_KEY: ${{ secrets.CACHIX_SIGNING_KEY }}
- name: "Run tests"
Expand Down
63 changes: 36 additions & 27 deletions build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
, writeText
, runCommandLocal
, remarshal
, crateDependencies
, formats
, cratesIoDependencies
, zstd
, fetchurl
, lndir
Expand All @@ -72,7 +73,7 @@
let
builtinz =
builtins // import ./builtins
{ inherit lib writeText remarshal runCommandLocal; };
{ inherit lib writeText remarshal runCommandLocal formats; };

drvAttrs = {
name = "${pname}-${version}";
Expand All @@ -83,15 +84,18 @@ let
postInstall
;

crate_sources = unpackedDependencies;
cratesio_sources = unpackedCratesIoDependencies;
git_sources = unpackedGitDependencies;

# The cargo config with source replacement. Replaces both crates.io crates
# and git dependencies.
cargoconfig = builtinz.toTOML {
cargoconfig = builtinz.writeTOML "config" {
source = {
crates-io = { replace-with = "nix-sources"; };
nix-sources = {
directory = unpackedDependencies;
crates-io = {
directory = unpackedCratesIoDependencies;
};
git = {
directory = unpackedGitDependencies;
};
} // lib.listToAttrs (
map
Expand All @@ -107,7 +111,7 @@ let
name = "${e.url}${key}";
value = lib.filterAttrs (n: _: n == "rev" || n == "tag" || n == "branch") e // {
git = e.url;
replace-with = "nix-sources";
replace-with = "git";
};
}
)
Expand Down Expand Up @@ -187,29 +191,33 @@ let
log "RUST_TEST_THREADS: $RUST_TEST_THREADS"
log "cargo_bins_jq_filter: $cargo_bins_jq_filter"
log "cargo_build_output_json (created): $cargo_build_output_json"
log "crate_sources: $crate_sources"
log "RUSTFLAGS: $RUSTFLAGS"
log "CARGO_BUILD_RUSTFLAGS: $CARGO_BUILD_RUSTFLAGS"

${lib.optionalString remapPathPrefix ''

# Remove the source path(s) in Rust
if [ -n "$RUSTFLAGS" ]; then
RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $crate_sources=/sources"
RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $cratesio_sources=/sources"
RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $git_sources=/sources"

log "RUSTFLAGS (updated): $RUSTFLAGS"
elif [ -n "$CARGO_BUILD_RUSTFLAGS" ]; then
CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS --remap-path-prefix $crate_sources=/sources"
log "CARGO_BUILD_RUSTFLAGS (updated): $CARGO_BUILD_RUSTFLAGS"
else
export CARGO_BUILD_RUSTFLAGS="--remap-path-prefix $crate_sources=/sources"
if [ -z "$CARGO_BUILD_RUSTFLAGS" ]; then
export CARGO_BUILD_RUSTFLAGS=""
fi

CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS --remap-path-prefix $cratesio_sources=/sources"
CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS --remap-path-prefix $git_sources=/sources"

log "CARGO_BUILD_RUSTFLAGS (updated): $CARGO_BUILD_RUSTFLAGS"
fi

''}

mkdir -p target

# make sure that all source files are tagged as "recent" (since we write
# Make sure that all source files are tagged as "recent" (since we write
# some stubs here and there)
find . -type f -exec touch {} +

Expand All @@ -235,7 +243,7 @@ let
export CARGO_HOME=''${CARGO_HOME:-$PWD/.cargo-home}
mkdir -p $CARGO_HOME

echo "$cargoconfig" > $CARGO_HOME/config
cp "$cargoconfig" $CARGO_HOME/config

runHook postConfigure
'';
Expand Down Expand Up @@ -364,7 +372,7 @@ let
};
};

# Unpacks all dependencies required to compile user's crate.
# Crates.io dependencies required to compile user's crate.
#
# As an output, for each dependency, this derivation produces a subdirectory
# containing `.cargo-checksum.json` (required for Cargo to process the crate)
Expand All @@ -379,18 +387,19 @@ let
# something-else-1.2.3/src (-> /nix/store/...)
# ...
# ```
#
# (note that the actual crate format is not document, but in practice it's a
# gzipped tar.)
unpackedDependencies = symlinkJoinPassViaFile {
name = "dependencies";

paths =
(map unpackCrateDependency crateDependencies) ++
(map unpackGitDependency gitDependencies);
unpackedCratesIoDependencies = symlinkJoinPassViaFile {
name = "crates-io-dependencies";
paths = (map unpackCratesIoDependency cratesIoDependencies);
};

# Git dependencies required to compile user's crate; follows same format as
# the crates.io dependencies above.
unpackedGitDependencies = symlinkJoinPassViaFile {
name = "git-dependencies";
paths = (map unpackGitDependency gitDependencies);
};

unpackCrateDependency = { name, version, sha256 }:
unpackCratesIoDependency = { name, version, sha256 }:
let
crate = fetchurl {
inherit sha256;
Expand Down
44 changes: 42 additions & 2 deletions builtins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,52 @@
, writeText
, runCommandLocal
, remarshal
, formats
}:

rec
{
toTOML = import ./to-toml.nix { inherit lib; };
writeTOML = name: attrs: writeText name (toTOML attrs);
# Serializes given attrset into a TOML file.
#
# Usage:
# writeTOML path attrset
#
# On newer nixpkgs, this function invokes `lib.formats.toml` that nowadays
# handles all TOML documents properly.
#
# On older nixpkgs, where that serializer doesn't work correctly¹, we rely on
# a custom implementation (with its own tiny shortcomings²).
#
# TODO remove our custom serializer after nixpkgs v23 becomes more widely
# adopted
#
# ¹ e.g. cases like `[targets."cfg(\"something\")"]` are translated badly
# ² https://github.com/nix-community/naersk/issues/263
writeTOML =
let
our-impl =
let
to-toml = import ./to-toml.nix {
inherit lib;
};

in
name: value:
runCommandLocal name {
value = to-toml value;
passAsFile = [ "value" ];
} ''
cp "$valuePath" "$out"
cat "$out"
'';

nixpkgs-impl = (formats.toml { }).generate;

in
if builtins.compareVersions lib.version "22.11" <= 0 then
our-impl
else
nixpkgs-impl;

readTOML = usePure: f:
if usePure then
Expand Down
2 changes: 1 addition & 1 deletion config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ let
# version and sha256 of the crate
# Example:
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
crateDependencies = libb.mkVersions buildPlanConfig.cargolock;
cratesIoDependencies = libb.mkVersions buildPlanConfig.cargolock;
};

# config used when planning the builds
Expand Down
5 changes: 3 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
, lib
, lndir
, remarshal
, formats
, rsync
, runCommandLocal
, rustc
Expand All @@ -15,10 +16,10 @@
}@defaultBuildAttrs:

let
libb = import ./lib.nix { inherit lib writeText runCommandLocal remarshal; };
libb = import ./lib.nix { inherit lib writeText runCommandLocal remarshal formats; };

builtinz = builtins // import ./builtins
{ inherit lib writeText remarshal runCommandLocal; };
{ inherit lib writeText remarshal runCommandLocal formats; };

mkConfig = arg:
import ./config.nix { inherit lib arg libb builtinz; };
Expand Down
9 changes: 4 additions & 5 deletions lib.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{ lib, writeText, runCommandLocal, remarshal }:
{ lib, writeText, runCommandLocal, remarshal, formats }:
let
builtinz =
builtins // import ./builtins
{ inherit lib writeText remarshal runCommandLocal; };
{ inherit lib writeText remarshal runCommandLocal formats; };
in
rec
{
Expand Down Expand Up @@ -121,14 +121,13 @@ rec

# A very minimal 'src' which makes cargo happy nonetheless
dummySrc =
{ cargoconfig # string
{ cargoconfig # path
, cargotomls # list
, cargolock # attrset
, copySources # list of paths that should be copied to the output
, copySourcesFrom # path from which to copy ${copySources}
}:
let
config = writeText "config" cargoconfig;
cargolock' = builtinz.writeTOML "Cargo.lock" cargolock;

fixupCargoToml = cargotoml:
Expand All @@ -154,7 +153,7 @@ rec
{ inherit copySources copySourcesFrom cargotomlss; }
''
mkdir -p $out/.cargo
${lib.optionalString (! isNull cargoconfig) "cp ${config} $out/.cargo/config"}
${lib.optionalString (! isNull cargoconfig) "cp ${cargoconfig} $out/.cargo/config"}
cp ${cargolock'} $out/Cargo.lock
for tuple in $cargotomlss; do
Expand Down
Loading
Loading