2023-10-24 11:01:19 +02:00
|
|
|
{ lib, pkgs, formats, runCommand, dasel }:
|
2023-07-22 11:14:51 +02:00
|
|
|
let
|
|
|
|
daselBin = lib.getExe dasel;
|
|
|
|
|
|
|
|
inherit (lib)
|
|
|
|
last
|
|
|
|
optionalString
|
|
|
|
types
|
|
|
|
;
|
|
|
|
in
|
|
|
|
rec {
|
|
|
|
# Creates a transformer function that writes input data to disk, transformed
|
|
|
|
# by both the `input` and `output` arguments.
|
|
|
|
#
|
|
|
|
# Type: makeDataWriter :: input -> output -> nameOrPath -> data -> (any -> string) -> string -> string -> any -> derivation
|
|
|
|
#
|
|
|
|
# input :: T -> string: function that takes the nix data and returns a string
|
|
|
|
# output :: string: script that takes the $inputFile and write the result into $out
|
|
|
|
# nameOrPath :: string: if the name contains a / the files gets written to a sub-folder of $out. The derivation name is the basename of this argument.
|
|
|
|
# data :: T: the data that will be converted.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; };
|
|
|
|
# myConfig = writeJSON "config.json" { hello = "world"; }
|
|
|
|
#
|
2023-10-24 11:01:50 +02:00
|
|
|
makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data:
|
2023-07-22 11:14:51 +02:00
|
|
|
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
|
|
|
|
let
|
|
|
|
name = last (builtins.split "/" nameOrPath);
|
|
|
|
in
|
2023-07-28 06:39:21 +02:00
|
|
|
runCommand name
|
2023-07-22 11:14:51 +02:00
|
|
|
{
|
|
|
|
input = input data;
|
|
|
|
passAsFile = [ "input" ];
|
|
|
|
} ''
|
|
|
|
${output}
|
|
|
|
|
|
|
|
${optionalString (types.path.check nameOrPath) ''
|
|
|
|
mv $out tmp
|
|
|
|
mkdir -p $out/$(dirname "${nameOrPath}")
|
|
|
|
mv tmp $out/${nameOrPath}
|
|
|
|
''}
|
2023-10-24 11:01:19 +02:00
|
|
|
'');
|
2023-07-22 11:14:51 +02:00
|
|
|
|
2023-10-24 11:01:19 +02:00
|
|
|
inherit (pkgs) writeText;
|
2023-07-22 11:14:51 +02:00
|
|
|
|
|
|
|
# Writes the content to a JSON file.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeJSON "data.json" { hello = "world"; }
|
2023-10-24 11:01:19 +02:00
|
|
|
writeJSON = (pkgs.formats.json {}).generate;
|
2023-07-22 11:14:51 +02:00
|
|
|
|
|
|
|
# Writes the content to a TOML file.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeTOML "data.toml" { hello = "world"; }
|
2023-10-24 11:01:19 +02:00
|
|
|
writeTOML = (pkgs.formats.toml {}).generate;
|
2023-07-22 11:14:51 +02:00
|
|
|
|
|
|
|
# Writes the content to a YAML file.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeYAML "data.yaml" { hello = "world"; }
|
2023-10-24 11:01:19 +02:00
|
|
|
writeYAML = (pkgs.formats.yaml {}).generate;
|
2023-07-22 11:14:51 +02:00
|
|
|
}
|