Use TOML serializer from nixpkgs

Closes https://github.com/nix-community/naersk/issues/263
This commit is contained in:
Patryk Wychowaniec 2023-07-14 11:21:27 +02:00
parent d9a33d69a9
commit eb2bdf3b0f
9 changed files with 87 additions and 163 deletions

View file

@ -62,6 +62,7 @@
, writeText
, runCommandLocal
, remarshal
, formats
, crateDependencies
, zstd
, fetchurl
@ -72,7 +73,7 @@
let
builtinz =
builtins // import ./builtins
{ inherit lib writeText remarshal runCommandLocal; };
{ inherit lib writeText remarshal runCommandLocal formats; };
drvAttrs = {
name = "${pname}-${version}";
@ -87,7 +88,7 @@ let
# 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 = {
@ -235,7 +236,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
'';

View file

@ -3,12 +3,12 @@
, writeText
, runCommandLocal
, remarshal
, formats
}:
rec
{
toTOML = import ./to-toml.nix { inherit lib; };
writeTOML = name: attrs: writeText name (toTOML attrs);
writeTOML = (formats.toml { }).generate;
readTOML = usePure: f:
if usePure then

View file

@ -1,139 +0,0 @@
{ lib }:
let
inherit (lib)
length
elemAt
concatMap
all
concatLists
concatStringsSep
concatMapStringsSep
mapAttrsToList
;
inherit (builtins)
abort
match
toJSON
typeOf
;
quoteKey = k:
if match "[a-zA-Z]+" k == []
then k
else quoteString k;
quoteString = builtins.toJSON;
outputValInner = v:
let
ty = tomlTy v;
in
if ty == "set" then
let
vals = mapAttrsToList
(k': v': "${quoteKey k'} = ${outputValInner v'}") v;
valsStr = concatStringsSep ", " vals;
in
"{ ${valsStr} }" else
outputVal v;
outputVal = v:
let
ty = tomlTy v;
in
if (ty == "bool" || ty == "int") then
builtins.toJSON v
else
if ty == "string" then
quoteString v
else
if ty == "list" || ty == "list_of_attrs" then
let
vals = map quoteString v;
valsStr = concatStringsSep ", " vals;
in
"[ ${valsStr} ]"
else
if ty == "set" then
abort "unsupported set for not-inner value"
else abort "Not implemented: type ${ty}";
outputKeyValInner = k: v:
let
ty = tomlTy v;
in
if ty == "set" then
let
vals = mapAttrsToList
(k': v': "${quoteKey k'} = ${outputValInner v'}") v;
valsStr = concatStringsSep ", " vals;
in
[ "${quoteKey k} = { ${valsStr} }" ] else
outputKeyVal k v;
# Returns a list of strings; one string per line
outputKeyVal = k: v:
let
ty = tomlTy v;
in
if ty == "bool" || ty == "int" then
[ "${quoteKey k} = ${outputValInner v}" ]
else
if ty == "string" then
[ "${quoteKey k} = ${quoteString v}" ]
else
if ty == "list_of_attrs" then
concatMap (
inner:
[ "[[${k}]]" ] ++ (concatLists (mapAttrsToList outputKeyValInner inner))
) v
else
if ty == "list" then
let
vals = map quoteString v;
valsStr = concatStringsSep ", " vals;
in
[ "${quoteKey k} = [ ${valsStr} ]" ] else
if ty == "set" then
[ "[${k}]" ] ++ (concatLists (mapAttrsToList outputKeyValInner v))
else abort "Not implemented: type ${ty} for key ${k}";
tomlTy = x:
if typeOf x == "string" then "string" else
if typeOf x == "bool" then "bool" else
if typeOf x == "int" then "int" else
if typeOf x == "float" then "float" else
if typeOf x == "set" then
if lib.isDerivation x then "string" else "set" else
if typeOf x == "list" then
if length x == 0 then "list"
else
let
ty = typeOf (elemAt x 0);
in
#assert (all (v: typeOf v == ty) x);
if ty == "set" then "list_of_attrs" else "list"
else abort "Not implemented: toml type for ${typeOf x}";
toTOML = attrs:
assert (typeOf attrs == "set");
let
byTy = lib.foldl
(
acc: x:
let
ty = tomlTy x.v;
in
acc // { "${ty}" = (acc.${ty} or []) ++ [ x ]; }
)
{} (mapAttrsToList (k: v: { inherit k v; }) attrs);
in
concatMapStringsSep "\n"
(kv: concatStringsSep "\n" (outputKeyVal kv.k kv.v))
(
(byTy.string or []) ++ (byTy.int or []) ++ (byTy.float or []) ++ (byTy.list or []) ++ (byTy.list_of_attrs or []) ++ (byTy.set or [])
)
;
in
toTOML

View file

@ -5,6 +5,7 @@
, lib
, lndir
, remarshal
, formats
, rsync
, runCommandLocal
, rustc
@ -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; };

View file

@ -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
{
@ -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:
@ -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

View file

@ -1,14 +1,14 @@
{
"agent-rs": {
"branch": "paulliu/add-cargo-lock",
"branch": "main",
"description": "A collection of libraries and tools for building software around the Internet Computer, in Rust.",
"homepage": "https://sdk.dfinity.org/",
"owner": "ninegua",
"homepage": "",
"owner": "dfinity",
"repo": "agent-rs",
"rev": "4a22e590516bc79ec3c75a320f7941e7762ea098",
"sha256": "0sacddc34nlfgldqghlwchgzjki177h5dsgpmdv70cm8hfy0sg7l",
"rev": "6d923cbc918852ef5bfaeb08e63c86580aa80ffe",
"sha256": "12y28nmv9af8sj55s998wydy08h32gvxr9dbhfxhfilxdairh0a2",
"type": "tarball",
"url": "https://github.com/ninegua/agent-rs/archive/4a22e590516bc79ec3c75a320f7941e7762ea098.tar.gz",
"url": "https://github.com/dfinity/agent-rs/archive/6d923cbc918852ef5bfaeb08e63c86580aa80ffe.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"fenix": {
@ -65,10 +65,10 @@
"homepage": null,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "6c2318e451fc0eaf338fb461d9bfcc99869de758",
"sha256": "1djwkvjnn26v0xw1swwh7dgmi0yl0b1kb8kansrdwk0jhhqbl7zq",
"rev": "1df733d83081fe79c109b066c90badece6b8d8b1",
"sha256": "1m3s4xv35jilrmbv8hj9gzpbcqgb8vb0iqhzcm1261x8b1hiylvj",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/6c2318e451fc0eaf338fb461d9bfcc99869de758.tar.gz",
"url": "https://github.com/NixOS/nixpkgs/archive/1df733d83081fe79c109b066c90badece6b8d8b1.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-21.05": {
@ -119,6 +119,18 @@
"url": "https://github.com/nushell/nushell/archive/85bfdca578157072e51e6972d370cfe63b0fda77.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"probe-rs": {
"branch": "master",
"description": "A debugging toolset and library for debugging embedded ARM and RISC-V targets on a separate host",
"homepage": "https://probe.rs",
"owner": "probe-rs",
"repo": "probe-rs",
"rev": "51fa324aef9f7c413988a3d18052b1bbc278a4c5",
"sha256": "0zb9s80hsz83ngjngs9cllp7gf8xq9jz0m3lwdhf08x3cp3bj6fd",
"type": "tarball",
"url": "https://github.com/probe-rs/probe-rs/archive/51fa324aef9f7c413988a3d18052b1bbc278a4c5.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"ripgrep-all": {
"branch": "master",
"description": "rga: ripgrep, but also search in PDFs, E-Books, Office documents, zip, tar.gz, etc.",

View file

@ -2,9 +2,25 @@
let
sources = import ../nix/sources.nix;
pkgs = import ../nix {
inherit system nixpkgs;
};
pkgs =
let
pkgs' = import ../nix {
inherit system nixpkgs;
};
older-pkgs = import ../nix {
inherit system;
nixpkgs = "nixpkgs-21.05";
};
in
pkgs' // {
# Some of our tests use dynamically-built Git repositories that fail extra
# security checks introduced in newer Git versions - so for the time being
# let's pin our test-Git to an older version.
git = older-pkgs.git;
};
naersk = pkgs.callPackage ../default.nix {
inherit (pkgs.rustPackages) cargo rustc;

View file

@ -2,6 +2,7 @@ args: {
agent-rs = import ./agent-rs args;
lorri = import ./lorri args;
nushell = import ./nushell args;
probe-rs = import ./probe-rs args;
ripgrep-all = import ./ripgrep-all args;
rustlings = import ./rustlings args;
talent-plan = import ./talent-plan args;

View file

@ -0,0 +1,33 @@
{ sources, pkgs, ... }:
let
fenix = import sources.fenix { };
toolchain = (fenix.toolchainOf {
channel = "nightly";
date = "2023-07-01";
sha256 = "sha256-pWd4tAHP4QWGC3CKWZzDjzYANxATC6CGRmKuP2ZZv5k=";
}).toolchain;
naersk = pkgs.callPackage ../../../default.nix {
cargo = toolchain;
rustc = toolchain;
};
app = naersk.buildPackage {
src = sources.probe-rs;
buildInputs = with pkgs; [
pkg-config
libusb1
openssl
] ++ lib.optionals stdenv.isDarwin [
darwin.DarwinTools
darwin.apple_sdk.frameworks.AppKit
];
};
in
pkgs.runCommand "probe-rs-test"
{
buildInputs = [ app ];
} "rtthost --help && touch $out"