labs/lib/src/packages/default.nix

58 lines
1.9 KiB
Nix
Raw Normal View History

2024-06-01 11:00:53 +00:00
lib: {
packages = {
2024-06-04 00:03:28 +00:00
## Check whether a value is a derivation. Note that this will also return true
## for "fake" derivations which are constructed by helpers such as
## `lib.paths.into.drv` for convenience.
##
## @type a -> Bool
isDerivation = value:
value.type or null == "derivation";
2024-06-01 11:00:53 +00:00
2024-06-04 00:03:28 +00:00
## Sanitize a string to produce a valid name for a derivation.
##
## @type String -> String
2024-06-01 11:00:53 +00:00
sanitizeDerivationName = let
validate = builtins.match "[[:alnum:]+_?=-][[:alnum:]+._?=-]*";
in
value:
# First detect the common case of already valid strings, to speed those up
if builtins.stringLength value <= 207 && validate value != null
then builtins.unsafeDiscardStringContext value
else
2024-06-03 09:57:13 +00:00
lib.fp.pipe [
2024-06-01 11:00:53 +00:00
# Get rid of string context. This is safe under the assumption that the
# resulting string is only used as a derivation name
builtins.unsafeDiscardStringContext
# Strip all leading "."
(x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0)
(lib.strings.split "[^[:alnum:]+._?=-]+")
# Replace invalid character ranges with a "-"
(lib.strings.concatMap (x:
if builtins.isList x
then "-"
else x))
# Limit to 211 characters (minus 4 chars for ".drv")
(x: builtins.substring (lib.math.max (builtins.stringLength x - 207) 0) (-1) x)
# If the result is empty, replace it with "unknown"
(x:
if builtins.stringLength x == 0
then "unknown"
else x)
2024-06-03 09:57:13 +00:00
]
value;
2024-06-05 02:04:40 +00:00
## Get an output of a derivation.
2024-06-05 02:04:40 +00:00
##
## @type String -> Derivation -> String
2024-06-05 02:04:40 +00:00
getOutput = output: package:
if ! package ? outputSpecified || !package.outputSpecified
then package.${output} or package.out or package
else package;
2024-06-01 11:00:53 +00:00
};
}