Commit graph

349 commits

Author SHA1 Message Date
Silvan Mosberger d064d972f0 lib.filesystem.pathType: Improve error for non-existent paths
Previously it would fail with

  error: attribute 'nonexistent' missing

         at nixpkgs/lib/filesystem.nix:29:10:

             28|     if dirOf path == path then "directory"
             29|     else (readDir (dirOf path)).${baseNameOf path};
               |          ^
             30|
2023-05-22 14:13:57 +02:00
Silvan Mosberger bb6eab0bdb lib.filesystem.pathType: Fix for filesystem root argument
Previously this function couldn't handle / being passed, it would throw
an error:

error: attribute '' missing

       at nixpkgs/lib/filesystem.nix:24:20:

           23|   */
           24|   pathType = path: (readDir (dirOf path)).${baseNameOf path};
             |                    ^
           25|

Consequently this also fixes the
lib.filesystem.{pathIsDirectory,pathIsRegularFile} functions.
2023-05-22 14:13:57 +02:00
Silvan Mosberger a1dedc908d lib.filesystem.pathType and co.: Add tests
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
2023-05-22 14:13:45 +02:00
Naïm Favier ac9915b1ea
lib/tests: add mkPackageOption tests 2023-05-20 18:23:41 +02:00
Robert Hensing eab660d91e lib.modules: configurationClass -> class
This simplifies the documentation. `configuration` is implied by `_type`.
2023-05-06 18:32:59 +02:00
Robert Hensing 89491bef8d lib.modules: in evalModules return move _module.class -> configurationClass 2023-05-06 18:32:59 +02:00
Robert Hensing fd88c79418 lib.modules: Change class declaration in module to _class 2023-05-06 18:32:59 +02:00
Robert Hensing 7459c02495 lib/tests/modules.sh: Add submodule + class tests 2023-05-06 18:32:59 +02:00
Robert Hensing 8054785157 lib/modules: Move class out of specialArgs 2023-05-06 18:32:58 +02:00
Robert Hensing 84b1b01702 lib/modules: Only interpret class declaration in non-shorthand mode
This is to avoid stealing keys from submodules. `class` might be
common enough that reinterpreting existing `class` attributes in
configurations as a declaration leads to fairly widespread problems.
2023-05-06 18:29:04 +02:00
Robert Hensing 9714487f74 lib/modules: Explain that a configuration can't be loaded as a module 2023-05-06 18:29:04 +02:00
Robert Hensing 2e689d58cb lib/modules: Improve error when a configuration is imported
This is appears to be a fairly common mistake for beginners who want
to build larger things from the system configurations, such as NixOps
networks, etc. Further explanation seems appropriate.
2023-05-06 18:29:03 +02:00
Robert Hensing 58f385f680 lib/modules: Check against importing things with a _type 2023-05-06 18:29:03 +02:00
Robert Hensing b8ff2807a2 lib/modules: Add class concept to check imports
This improves the error message when an incompatible module is
imported.
2023-05-06 18:29:03 +02:00
Robert Hensing e7e64233c9 lib/tests/modules.sh: Unload implicit modules
I had some trouble understanding this. Let's try to keep new tests
a bit more stateless and explicit.
2023-05-06 18:22:50 +02:00
toonn e31c8b22dd
Merge pull request #223407 from AngryAnt/toplist-path
lib.toPlist: Add support for path values
2023-05-05 21:15:23 +02:00
Matthieu Coudron 8670e496ff
Merge pull request #227714 from ony/feature/generateLuarocksConfig-toLua
lua.lib: use toLua in generateLuarocksConfig
2023-05-01 18:53:22 +02:00
Mykola Orliuk 7287c0e076 lib.generators.toLua: asBindings option
Allows to generate code block for setting of global variables
2023-04-29 19:26:35 +02:00
Weijia Wang b2ef7956b6
Merge pull request #227560 from jackyliu16/loongnix-commit
lib.platforms.loongarch64: init
2023-04-28 13:21:42 +03:00
jackyliu16 edcad332d9 lib.platforms.loongarch64: init 2023-04-27 20:04:30 +03:00
Mykola Orliuk e9b416168a lib.generators.toLua: allow disabling multiline 2023-04-23 19:46:14 +02:00
Mykola Orliuk 4ec4c6fda9 lib/generators: add toLua/mkLuaInline
Suitable to simplify Lua-based configurations like neovim-lspconfig that
might need to interpolate Nix package paths.
2023-04-23 01:07:58 +02:00
Emil "AngryAnt" Johansen e932e98437
lib.toPlist: keep test output in external files for their tab indents 2023-03-27 19:25:52 +02:00
Emil "AngryAnt" Johansen 63a8c43d09
lib.toPlist: basic test coverage 2023-03-27 19:25:38 +02:00
Naïm Favier 8751764236
lib/modules: better error for invalid option declarations
Make `byName` aware of whether it's processing options or config to give
slightly more accurate error messages.
2023-03-22 12:37:28 +01:00
hsjobeki 15a8d05ba5 init: lib.foldlAttrs
- provide comprehensive example
- add unit test
2023-03-11 10:42:00 +01:00
Robert Hensing 6b79fe8cdc
Merge pull request #211855 from hercules-ci/lib-modules-disabledModules-module-with-key
lib/modules: Allow an "anonymous" module with key in disabledModules
2023-03-09 16:16:37 +01:00
Patrick Widmer 7089294f10 strings: add escapeQuery for url encoding 2023-03-03 20:48:55 +01:00
Robert Hensing 118bdf25a6 lib/modules: Allow an "anonymous" module with key in disabledModules
This makes the following work

    disabledModules = [ foo.nixosModules.bar ];

even if `bar` is not a path, but rather a module such as

    { key = "/path/to/foo#nixosModules.bar"; config = ...; }

By supporting this, the user will often be able to use the same syntax
for both importing and disabling a module. This is becoming more relevant
because flakes promote the use of attributes to reference modules. Not
all of these modules in flake attributes will be identifiable, but with
the help of a framework such as flake-parts, these attributes can be
guaranteed to be identifiable (by outPath + attribute path).
2023-03-01 15:03:44 +01:00
Silvan Mosberger 0a60663e67
Merge pull request #206611 from h7x4/lib-lists-add-repeat
lib.lists: add `replicate`
2023-02-07 06:16:09 +01:00
h7x4 7c4abbf80e
lib.lists: add replicate
`replicate` returns n copies of an element as a list.

Co-Authored-By: Silvan Mosberger <contact@infinisil.com>
2023-02-06 20:40:47 +01:00
Silvan Mosberger 50e4dbf35b
Merge pull request #205557 from ncfavier/concatLines
lib/strings: add `concatLines`
2023-01-31 18:05:53 +01:00
Naïm Favier 0288ee587f
maintainers: clarify what fields are required 2023-01-27 18:05:28 +01:00
Naïm Favier 93dc2de29a
maintainers: disallow noreply.github.com emails 2023-01-27 18:05:28 +01:00
Naïm Favier 4e39849611
maintainers: make email optional
Not giving an email address is fine as long as the maintainer is
reachable through other means, such as GitHub or Matrix.
2023-01-27 18:04:20 +01:00
John Ericson c1071e7804
Merge pull request #211622 from alyssais/valgrind-platforms
valgrind: make meta.platforms more accurate
2023-01-19 15:43:22 -05:00
Alyssa Ross 6d165a9474
lib.platforms.s390x: init 2023-01-19 17:43:50 +00:00
Alyssa Ross 541a2a5e91
lib.platforms.power: init 2023-01-19 17:43:49 +00:00
Alyssa Ross 48f3fd2d49
lib.platforms.armv7: init 2023-01-19 17:43:41 +00:00
Robert Hensing 415504e867 lib/tests/release.nix: Make nix a parameter + strictDeps
This makes bisecting nix a bit easier.

Example reproducer, invoked from nix directory:

```bash
nix-build ../nixpkgs/lib/tests/release.nix --arg nix '(builtins.getFlake "git+file://${toString ./.}").packages.x86_64-linux.default'
```
2023-01-18 01:39:38 +01:00
Robert Hensing f61d4d346b
Merge pull request #205190 from NixOS/lib.path.relativeNormalise
lib.path.subpath.{isValid,normalise}: init
2023-01-03 13:46:11 +01:00
Silvan Mosberger 98fbcf1788 lib.path.subpath.isValid: init
The first path library function
2023-01-03 13:20:36 +01:00
Naïm Favier 0355479715
lib/versions: add pad
Pad a version string with zeros to match a given number of components.
2022-12-21 12:58:21 +01:00
figsoda 695d4bc76b lib: fix typos 2022-12-17 18:59:29 -05:00
Robert Hensing efa1140e83
Merge pull request #205457 from h7x4/lib-strings-toInt-broken-for-negative-numbers
lib.strings: fix negative number handling for `toInt` and `toIntBase10`
2022-12-11 02:26:42 +01:00
Naïm Favier ed0b8c26f1
lib/strings: add concatLines
Like `unlines` from Haskell.

The aim is to replace the `concatStringsSep "\n"` pattern for generated
files, which doesn't add a final newline.
2022-12-10 15:56:30 +01:00
h7x4 62e863e98c
lib.strings: fix negative number handling for toInt and toIntBase10
The previous version would be unstable due to an input validation regex
not expecting a '-' in front of the number.
2022-12-10 13:16:45 +01:00
Naïm Favier 6a117e2759 nixos/doc: render option values using lib.generators.toPretty
Render un`_type`d defaults and examples as `literalExpression`s using
`lib.generators.toPretty` so that consumers don't have to reinvent Nix
pretty-printing. `renderOptionValue` is kept internal for now intentionally.

Make `toPretty` print floats as valid Nix values (without a tilde).

Get rid of the now-obsolete `substSpecial` function.

Move towards disallowing evaluation of packages in the manual by
raising a warning on `pkgs.foo.{outPath,drvPath}`; later, this should
throw an error. Instead, module authors should use `literalExpression`
and `mkPackageOption`.
2022-12-08 17:52:52 +01:00
Naïm Favier 0b661ce32a lib/generators.toPretty: escape strings properly 2022-12-08 17:52:52 +01:00
Naïm Favier 0fa7b1b004 lib/generators.toPretty: don't evaluate derivations
With the goal of making `toPretty` suitable for rendering option
values, render derivations as `<derivation foo-1.0>` instead of
`<derivation /nix/store/…-foo-1.0.drv>`.

This is to avoid causing sudden evaluation errors for out-of-tree
projects that have options with `default = pkgs.someUnfreePackage;` and
no `defaultText`.
2022-12-08 17:52:52 +01:00
Robert Hensing 6110a6009f lib/modules: Add context to the "option does not exist" error
Add trace items that provide context for a failed definition that
can not be caught within the Nix language.

This also adds a test for the `tryEval` behavior of `showDefs`.
2022-12-02 11:06:53 +00:00
figsoda f993f8a186 lib/attrsets: add concatMapAttrs 2022-11-17 10:41:53 -05:00
John Ericson 66aa02f190 lib/systems: Support FreeBSD
A tricky thing about FreeBSD is that there is no stable ABI across
versions. That means that putting in the version as part of the config
string is paramount.

We have a parsed represenation that separates name versus version to
accomplish this. We include FreeBSD versions 12 and 13 to demonstrate
how it works.
2022-11-04 16:49:28 -04:00
Robert Hensing 1b6e5ac952 lib/tests/modules: Test doRename 2022-11-03 13:12:58 +01:00
Jacob Abel ed71173841
lib/strings: Update docs and restructured code to improve readability of toInt and toIntBase10. 2022-10-23 17:50:24 -04:00
Jacob Abel 39a4ab78a1
lib/strings: Refactor toInt into toInt and toIntBase10 2022-10-23 17:50:24 -04:00
Jacob Abel 3d196a5f2a
lib/strings: Update toInt to handle intermixed ws and zeros. Added tests 2022-10-23 17:50:23 -04:00
Jacob Abel febff1dccd
lib/strings: allow toInt to parse zero-padded strings 2022-10-23 17:50:20 -04:00
Daniel Olsen 23c1754fff lib/tests/misc: Add tests for charToInt, escapeC, and normalizePath 2022-10-20 20:12:15 +02:00
Maximilian Bosch ac48f07282
lib/types: always use <function body> instead of [function body] to indicate a function inside an option structure
The motivation is to have a single identifier for that. Useful for the
next commit where I'll try to escape option-parts correctly (options can
be any kind of strings, but unless these are Nix identifiers, they must
be quoted).

Since `<function body>` (or `<name>`/`*`) are special identifiers in
error messages and the manual, we need a unique way to mark an option
part as function call because these are not to be quoted.
2022-10-07 10:01:44 +02:00
figsoda a1d50eecab
Merge pull request #193132 from figsoda/clean-up
treewide: clean up
2022-10-01 17:03:11 -04:00
figsoda f9f6872960 lib/tests/maintainers.nix: remove unused binding 2022-09-26 22:18:55 -04:00
Robert Hensing 1ffa30b055 lib/modules: Fix meta duplication in shorthand syntax 2022-09-21 10:55:11 +01:00
Robert Hensing fce8b018f0 lib: Add lazyDerivation 2022-09-21 10:55:07 +01:00
Robert Hensing 1cbe950384 lib.types: Add parentheses where description is ambiguous 2022-09-17 22:16:39 +01:00
Robert Hensing 3ebb588ab3
Merge pull request #188289 from erikarvstedt/fix-disabled-modules-abs-paths
lib.modules: support strings with absolute paths in `disabledModules`
2022-08-31 15:27:03 +02:00
Erik Arvstedt e2cc361970
lib.modules: support strings with absolute paths in disabledModules
This is particularly useful for disabling modules defined in a flake.
Example:
disabledModules = [ "${flake}/modules/mymodule.nix" ];

Previously, absolute string paths were internally prepended with `modulesPath`,
which caused the module filtering to fail.
2022-08-31 14:23:28 +02:00
Minijackson 4db467f7e9
lib/systems: add MicroBlaze architectures 2022-08-25 16:00:42 +02:00
Graham Christensen 9ed793229c teams/maintainers list: show instructions for validating the contents 2022-06-20 22:20:26 +02:00
Graham Christensen ff38ee15c2 maintainer teams: check them in lib tests 2022-06-20 22:20:26 +02:00
Graham Christensen 3ac995a568 maintainer lib test: extract maintainer module 2022-06-20 22:20:26 +02:00
Graham Christensen c8cebff38b maintainers: remove longkeyid
see https://dkg.fifthhorseman.net/blog/openpgp-key-ids-are-not-useful.html
2022-06-20 22:20:26 +02:00
Robert Hensing 3c4a49f506 lib/modules: Throw earlier when module function does not return attrs
`m` must always be an attrset at this point. It is basically always
evaluated. This will make it throw when any of the attrs is accessed,
rather than just `config`. We assume that this will improve the error
message in more scenarios.
2022-06-14 23:23:41 +02:00
Robert Hensing dfd98a5da2 lib.deferredModule: Make it properly singular 2022-06-14 23:01:27 +02:00
Robert Hensing 781c2e0789 lib.types.deferredModule: Allow path-typed module references 2022-06-14 23:01:26 +02:00
Robert Hensing a2c29561e7 lib.types.deferredModule: Improve reported location 2022-06-14 23:01:26 +02:00
Robert Hensing 38b7709a6f lib/test/modules.sh: Test deferredModule error location file 2022-06-14 23:01:26 +02:00
Robert Hensing 4746f6d03e lib.types: Add deferredModule 2022-06-14 23:01:23 +02:00
Silvan Mosberger 79441600c2 lib/tests: Add submodule file propagation test 2022-06-14 17:01:58 +02:00
Silvan Mosberger 4b2827e6a1
Merge pull request #168374 from Ma27/special-attrs-in-with-recursion
lib/generators: withRecursion: don't break attr-sets with special attrs
2022-05-23 17:55:18 +02:00
Robert Hensing 2d1a34b8cc
Merge pull request #172813 from hercules-ci/functionTo-properly
`lib.types.functionTo` type merging and docs
2022-05-17 22:22:21 +02:00
Silvan Mosberger e06f66e73c
Merge pull request #170561 from klemensn/types-description
lib/types: Drop misleading plural from type descriptions
2022-05-16 17:03:37 +02:00
adisbladis 161776ec1e Revert "lib: init flakes.nix"
This reverts commit PR #167947.

Flakes aren't standardised and the `lib` namespace shouldn't be
polluted with utilities that serve only experimental uses.
2022-05-15 22:39:46 +12:00
Robert Hensing 81a0a8be29 lib/tests/modules: Test functionTo submodule merging too 2022-05-13 09:09:16 +02:00
Robert Hensing 06da97fc3a lib.types.functionTo: Support type merging 2022-05-13 09:01:05 +02:00
Naïm Favier 4d2ea62d82
lib/strings/toShellVars: handle derivations as strings 2022-05-07 17:01:51 +02:00
Robert Hensing 9252a7daa8 lib/tests/modules.sh: Fix for singular type descriptions 2022-05-02 10:41:47 +02:00
Robert Hensing 761c21a16a
Merge pull request #170090 from danth/has-infix-toString
lib/strings: call toString within hasInfix
2022-05-02 09:47:15 +02:00
Daniel Thwaites 7b5be1a0f8 lib/tests: add tests for hasInfix
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2022-05-02 09:05:56 +02:00
Artturi a5357d06e4
Merge pull request #167947 from MatthewCroughan/mc/callLocklessFlake
lib: add callLocklessFlake
2022-05-01 23:06:17 +03:00
Alyssa Ross 2a6288d9b9 lib.systems: add riscv{32,64} sets and filters
For other platforms like Intel and ARM, we can do
e.g. lib.platforms.aarch64 to get only the 64-bit ARM platorms, but
until now there were no equivalents for RISC-V.
2022-04-28 08:17:02 +00:00
Alyssa Ross ed24c902d0 lib/tests: add RISC-V test 2022-04-28 08:17:02 +00:00
Naïm Favier 226bc99659
lib/strings: add toShellVars
A straightforward piece of plumbing to safely inject Nix variables into
shell scripts:

''
  ${lib.toShellVars { inherit foo bar; }}
  cmd "$foo" --bar "$bar"
''
2022-04-27 16:04:17 +02:00
Robert Hensing 224426ba6d lib.types.submoduleWith: Avoid _key collisions after extendModules 2022-04-24 00:07:59 +02:00
matthewcroughan 3f128cc024 lib/tests: evaluate value from subflake with callLocklessFlake 2022-04-12 21:22:36 +01:00
matthewcroughan ec59145c3b lib/tests: use subflake to test callLocklessFlake 2022-04-12 20:38:55 +01:00
matthewcroughan cc052779fb lib/tests: add test for callLocklessFlake 2022-04-12 19:47:48 +01:00
Maximilian Bosch 7203788068
lib/generators: withRecursion: don't break attr-sets with special attrs
Closes #168327

The issue reported there can be demonstrated with the following
expression:

    → nix-instantiate --eval -E "with import ./. {}; pkgs.lib.options.showDefs [ { file = \"foo\"; value = pkgs.rust.packages.stable.buildRustPackages; } ]"
    error: attempt to call something which is not a function but a string

           at /home/ma27/Projects/nixpkgs/lib/trivial.nix:442:35:

              441|   isFunction = f: builtins.isFunction f ||
              442|     (f ? __functor && isFunction (f.__functor f));
                 |                                   ^
              443|

Basically, if a `__functor` is in an attribute-set at depth-limit,
`__functor` will be set to `"<unevaluated>"`. This however breaks
`lib.isFunction` which checks for a `__functor` by invoking `__functor`
with `f` itself.

The same issue - "magic" attributes being shadowed by `withRecursion` -
also applies to others such as
`__pretty`/`__functionArgs`/`__toString`.

Since these attributes have a low-risk of causing a stack overflow
(because these are flat attr-sets or even functions), ignoring them in
`withRecursion` seems like a valid solution.
2022-04-12 12:34:23 +02:00
Silvan Mosberger 180173a1c4
Merge pull request #164088 from Profpatsch/lib.generators-add-toINIWithGlobalSection
lib.generators: add toINIWithGlobalSection
2022-04-06 19:02:36 +02:00
Robert Hensing c705953267
Merge pull request #165540 from Infinisil/module-args-docs
lib/modules: Document `_module.args`
2022-04-05 21:51:46 +02:00