doc: improve pkgs.writers comments

This commit is contained in:
Johannes Kirschbauer 2024-02-29 10:38:03 +01:00
parent 33b23ce735
commit 54658a47d0
No known key found for this signature in database

View file

@ -1,28 +1,34 @@
{ lib, pkgs, formats, runCommand, dasel }: { lib, pkgs, formats, runCommand }:
let let
daselBin = lib.getExe dasel;
inherit (lib) inherit (lib)
last last
optionalString optionalString
types types
; ;
in in
rec { {
# Creates a transformer function that writes input data to disk, transformed /**
# by both the `input` and `output` arguments. 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
# # Example
# 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 ```nix
# 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. writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; };
# data :: T: the data that will be converted. myConfig = writeJSON "config.json" { hello = "world"; }
# ```
# Example:
# writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; }; # Type
# myConfig = writeJSON "config.json" { hello = "world"; }
# ```
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.
```
*/
makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data:
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
let let
@ -44,21 +50,36 @@ rec {
inherit (pkgs) writeText; inherit (pkgs) writeText;
# Writes the content to a JSON file. /**
# Writes the content to a JSON file.
# Example:
# writeJSON "data.json" { hello = "world"; } # Example
```nix
writeJSON "data.json" { hello = "world"; }
```
*/
writeJSON = (pkgs.formats.json {}).generate; writeJSON = (pkgs.formats.json {}).generate;
# Writes the content to a TOML file. /**
# Writes the content to a TOML file.
# Example:
# writeTOML "data.toml" { hello = "world"; } # Example
```nix
writeTOML "data.toml" { hello = "world"; }
```
*/
writeTOML = (pkgs.formats.toml {}).generate; writeTOML = (pkgs.formats.toml {}).generate;
# Writes the content to a YAML file. /**
# Writes the content to a YAML file.
# Example:
# writeYAML "data.yaml" { hello = "world"; } # Example
```nix
writeYAML "data.yaml" { hello = "world"; }
```
*/
writeYAML = (pkgs.formats.yaml {}).generate; writeYAML = (pkgs.formats.yaml {}).generate;
} }