Skip to content

Commit

Permalink
chore: formatted everything with alejandra
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelSilverdew committed Oct 27, 2023
1 parent aeb58d5 commit 54219f2
Show file tree
Hide file tree
Showing 47 changed files with 1,416 additions and 1,227 deletions.
364 changes: 195 additions & 169 deletions build.nix

Large diffs are not rendered by default.

114 changes: 55 additions & 59 deletions builtins/default.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# some extra "builtins"
{ lib
, writeText
, runCommandLocal
, remarshal
, formats
}:

rec
{
lib,
writeText,
runCommandLocal,
remarshal,
formats,
}: rec
{
# Serializes given attrset into a TOML file.
#
Expand All @@ -24,34 +23,30 @@ rec
#
# ¹ 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;

writeTOML = let
our-impl = let
to-toml = import ./to-toml.nix {
inherit lib;
};
in
if builtins.compareVersions lib.version "22.11" <= 0 then
our-impl
else
nixpkgs-impl;
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
if usePure
then
builtins.fromTOML (
# Safety: We invoke `unsafeDiscardStringContext` _after_ reading the
# file, so the derivation either had been already realized or `readFile`
Expand All @@ -76,16 +71,16 @@ rec
builtins.fromJSON (
builtins.readFile (
runCommandLocal "from-toml" {
buildInputs = [ remarshal ];
buildInputs = [remarshal];
}
''
echo "$from_toml_in" > in.toml
remarshal \
-if toml \
-i ${f} \
-of json \
-o $out
''
''
echo "$from_toml_in" > in.toml
remarshal \
-if toml \
-i ${f} \
-of json \
-o $out
''
)
);

Expand All @@ -95,24 +90,25 @@ rec
# TODO: use `builtins.pathExists` once
# https://github.com/NixOS/nix/pull/3012 has landed and is generally
# available
pathExists = if lib.versionAtLeast builtins.nixVersion "2.3" then builtins.pathExists else path:
let
pathExists =
if lib.versionAtLeast builtins.nixVersion "2.3"
then builtins.pathExists
else path: let
all = lib.all (x: x);
isOk = part:
let
dir = builtins.dirOf part;
basename = builtins.unsafeDiscardStringContext (builtins.baseNameOf part);
dirContent = builtins.readDir dir;
in
builtins.hasAttr basename dirContent && # XXX: this may not work if the directory is a symlink
(part == path || dirContent.${basename} == "directory");
parts =
let
# [ "" "nix" "store" "123123" "foo" "bar" ]
parts = lib.splitString "/" path;
len = lib.length parts;
in
map (n: lib.concatStringsSep "/" (lib.take n parts)) (lib.range 3 len);
isOk = part: let
dir = builtins.dirOf part;
basename = builtins.unsafeDiscardStringContext (builtins.baseNameOf part);
dirContent = builtins.readDir dir;
in
builtins.hasAttr basename dirContent
&& # XXX: this may not work if the directory is a symlink
(part == path || dirContent.${basename} == "directory");
parts = let
# [ "" "nix" "store" "123123" "foo" "bar" ]
parts = lib.splitString "/" path;
len = lib.length parts;
in
map (n: lib.concatStringsSep "/" (lib.take n parts)) (lib.range 3 len);
in
all (map isOk parts);
}
207 changes: 104 additions & 103 deletions builtins/to-toml.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib }:
let
inherit (lib)
{lib}: let
inherit
(lib)
length
elemAt
concatMap
Expand All @@ -11,7 +11,8 @@ let
mapAttrsToList
;

inherit (builtins)
inherit
(builtins)
abort
match
toJSON
Expand All @@ -25,115 +26,115 @@ let

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;
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}";
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;
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}";
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}";
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
assert (typeOf attrs == "set"); let
byTy =
lib.foldl
(
acc: x:
let
ty = tomlTy x.v;
in
acc // { "${ty}" = (acc.${ty} or []) ++ [ x ]; }
acc: x: let
ty = tomlTy x.v;
in
acc // {"${ty}" = (acc.${ty} or []) ++ [x];}
)
{} (mapAttrsToList (k: v: { inherit k v; }) attrs);
{} (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 [])
)
;
(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
toTOML
Loading

0 comments on commit 54219f2

Please sign in to comment.