Commit graph

37 commits

Author SHA1 Message Date
Philip Taron 4f652cfaf4
lib.filesystem: remove Nix 2.14 shim
It was added in fcaa2b1097. Lix is based off of 2.18, and the minimum version for Nixpkgs has long since incorporated this builtin.
2025-10-09 12:23:38 -07:00
Eman Resu ca59048a42 lib.filesystem.listFilesRecursive: only flatten once 2025-10-07 21:12:46 -04:00
Rebecca Turner 4a81a5e556
lib.packagesFromDirectoryRecursive: Allow non-"path" directory
As initially designed, `lib.packagesFromDirectoryRecursive` allowed
passing a string for the `directory` argument. This is necessary for
several reasons:

- `outPath` on derivations and Flake inputs is not a path.
- Derivations can be coerced to their `outPath` in string interpolation,
  but that produces strings, not paths.
- `builtins.path`, bizarrely, returns a string instead of a path (not
  that the documentation makes this clear).

If a path is used instead of a string here, then Nix will dutifully copy
the entire directory into a new path in the Nix store (ignored as
WONTFIX by Eelco in https://github.com/NixOS/nix/issues/9428). For
industrial use cases, this can result in an extra 10-15 seconds on every
single eval just to copy files from one spot in the Nix store to another
spot in the Nix store.

In #361424, this was changed so that `directory` must be a path,
breaking these use-cases.

I'm not really sure what happened here -- #361424 has very little
justification for why it exists, only a reference to a previous version
of the PR (#359941), which itself had very little justification given.
The description on #359941 explained that it would "Shrink the
function's code by ~2/3rd 🎉", but 60% of the reduction in size was just
deleting comments (!) and bindings like `directoryEntryIsPackage` that
helped clarify the intent of the implementation. As a result, the new
implementation is (to my eyes) more challenging to read and understand.
I think the whole thing was in service of #392800, which adds a
`newScope` argument in order "to create nested scopes for each
(sub)directory (not just the top-level one) when `newScope` is given."

Nobody noticed this regression until after the commit was merged. After
@phanirithvij pointed out the regression, @nbraud said they would
"shortly prepare a PR to fix this" [1] but did not. Later, they would
explain that they were "quite ill the last month(s)" [2], which explains
why this got forgotten about. @nbraud also requested a review from
@Gabriella439 [3], as she had reviewed the original PR adding
`lib.packagesFromDirectoryRecursive`, but not from me, the original
author of that PR. @Gabriella439 did not review the "refactor" PR, and
no attempt to contact her or myself was made after that initial request.
This behavior is admittedly rather subtle, so I'm not sure either
Gabriella or myself would have noticed the change (especially since the
relevant PR restructures the entire implementation).

While I find this a bit frustrating, I should have added a test for this
use-case in my original PR; if there was a test that relied on passing
paths in as a string, perhaps the authors modifying this code would have
noticed that the implementation was not an accident.

[1]: https://github.com/NixOS/nixpkgs/pull/361424#discussion_r1912407693
[2]: https://github.com/NixOS/nixpkgs/pull/359984#issuecomment-2775768808
[3]: https://github.com/NixOS/nixpkgs/pull/361424#issuecomment-2521308983
2025-07-10 16:13:38 -07:00
Robert Hensing ecc039f327 lib.filesystem.resolveDefaultNix: init 2025-06-21 21:43:37 +02:00
Silvan Mosberger 374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00
nicoo 05d528b56b lib.packagesFromDirectoryRecursive: drop open recursion from API surface 2025-03-26 12:11:44 +01:00
nicoo 151b6e431a lib.packagesFromDirectoryRecursive: document that symlinks are ignored
This was implicit in the previous “other files are ignored.”
2025-03-26 12:11:44 +01:00
nicoo be6ce5f40d lib.packagesFromDirectoryRecursive: Add example use of recurseIntoDirectory 2025-03-26 12:11:44 +01:00
nicoo 93d76b7344 lib.packagesFromDirectoryRecursive: reject unknown arguments
see [discussion](https://github.com/NixOS/nixpkgs/pull/270537#discussion_r1862458435)
2025-03-26 12:11:44 +01:00
nicoo 6cc285d613 lib.packagesFromDirectoryRecursive: hoist processDir out of the lambda
Fixes a bug preventing `recurseIntoDirectory` from changing the `directory` argument.

Moreover, `processDir` now cannot capture arguments from the `packagesFromDirectoryRecursive` call,
entirely preventing this class of bug from reoccurring should new parameters be added etc.
2025-03-26 12:11:44 +01:00
nicoo 32235deeb8 lib.packagesFromDirectoryRecursive: default to creating new scopes when newScope is provided
Co-authored-by: Rebecca Turner <rbt@sent.as>
2025-03-26 12:11:44 +01:00
nicoo 6b7576b0cf lib.packagesFromDirectoryRecursive: use explicit recursion, support nested scopes 2025-03-26 12:11:43 +01:00
nicoo 325b3755f5 lib.filesystem: remove dead let-bindings
Found by `deadnix` 🖤
2025-03-24 18:35:25 +01:00
nicoo f91fbd1624
Merge #361424: refactor lib.packagesFromDirectoryRecursive (v2) 2024-12-28 21:11:11 +00:00
nicoo 065b48084f lib.packagesFromDirectoryRecursive: let-bind functions called from lib
I was told it is better for eval performance:
  https://github.com/NixOS/nixpkgs/pull/361424#discussion_r1879975642
2024-12-12 18:48:45 +00:00
nicoo 1db8650fb4 lib.packagesFromDirectoryRecursive: refactor, again
This is a second take on #359941, which was reverted by
  940db5766a.

Co-authored-by: Silvan Mosberger <github@infinisil.com>
2024-12-12 18:48:09 +00:00
nicoo 25bdcd51e8 lib.packagesFromDirectoryRecursive: Split and explain examples, warn about scope limitation 2024-12-03 11:01:28 +00:00
nicoo 781b44b39d lib.packagesFromDirectoryRecursive: document inputs better
Cut out redundant boilerplate, explain what the `callPackage` parameter is.

Co-authored-by: Valentin Gagarin <valentin@gagarin.work>
2024-12-03 11:01:12 +00:00
K900 940db5766a
Revert "lib.filesystem.packagesFromDirectoryRecursive: refactor (#359941)"
This reverts commit 8a59b79070.
2024-12-02 10:21:52 +03:00
nicoo 8a59b79070
lib.filesystem.packagesFromDirectoryRecursive: refactor (#359941)
No functional changes.

- Centralize the logic classifying files/directories of interest, instead of
  being spread between `directoryEntryIsPackage` and `directoryEntryToAttrPair`.
- Replace a composition of `mapAttrs'` and `filterAttrs` with `concatMapAttrs`.
- Simplify future improvements, such as creating nested scopes for subdirs,
  or ignoring unsupported files.
2024-12-01 10:07:46 +00:00
nicoo 13b953c864 lib.packagesFromDirectoryRecursive: More precise type signature
Pulled above the inputs section to avoid duplicating information.
2024-11-28 16:27:36 +00:00
Johannes Kirschbauer b5af504a3d
doc: migrate lib.filesystem to doc-comment format (#312222)
* doc: migrate lib.filesystem to doc-comment format

* defintion list fixes lib/filesystem.nix

Co-authored-by: Daniel Sidhion <DanielSidhion@users.noreply.github.com>

---------

Co-authored-by: Daniel Sidhion <DanielSidhion@users.noreply.github.com>
2024-06-26 22:01:28 +02:00
Rebecca Turner 090b929b8a
lib.packagesFromDirectoryRecursive: init
Co-authored-by: Gabriella Gonzalez <GenuineGabriella@gmail.com>
2023-12-19 09:48:17 -08:00
Silvan Mosberger 055ba65fed lib: Take advantage of section descriptions
See https://github.com/nix-community/nixdoc/releases/tag/v2.6.0
2023-11-20 03:02:11 +01:00
figsoda dafa046a1e lib/filesystem: remove unused let bindings 2023-06-23 09:41:04 +00:00
Silvan Mosberger 378bf1a619 lib/filesystem.nix: Update top comment
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
2023-05-22 14:17:42 +02:00
Silvan Mosberger fcaa2b1097 lib.filesystem.pathType: Use new builtins.readFileType if available
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
2023-05-22 14:13:57 +02:00
Silvan Mosberger 84a3d633d6 lib.filesystem.pathType and co.: Improve documentation 2023-05-22 14:13:57 +02:00
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 5346636c20 lib.filesystem: Minor refactor
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
2023-05-22 14:13:57 +02:00
Silvan Mosberger c701a4dd29 lib.sources.pathType and co.: Move to lib.filesystem
These functions only work with the filesystem, they don't import
anything as sources
2023-04-05 17:18:10 +02:00
Silvan Mosberger 9bfc4bbd63 lib: Automatically generate lib.filesytem docs 2022-10-26 15:10:47 +02:00
Farid Zakaria 5f1d1bc57e lib: Add readTree function to filesystem
Add a friendly function to easily return a flattened list of files
within a directory.

This is useful if you want to easily iterate or concatSep the list of
files all found within a directory.
(i.e. when constructing Java's CLASSPATH)

Style improvements

Co-authored-by: Silvan Mosberger <github@infinisil.com>
2020-10-19 16:42:21 -07:00
Graham Christensen 152c63c9ff
Convert libs to a fixed-point
This does break the API of being able to import any lib file and get
its libs, however I'm not sure people did this.

I made this while exploring being able to swap out docFn with a stub
in #2305, to avoid functor performance problems. I don't know if that
is going to move forward (or if it is a problem or not,) but after
doing all this work figured I'd put it up anyway :)

Two notable advantages to this approach:

1. when a lib inherits another lib's functions, it doesn't
   automatically get put in to the scope of lib
2. when a lib implements a new obscure functions, it doesn't
   automatically get put in to the scope of lib

Using the test script (later in this commit) I got the following diff
on the API:

  + diff master fixed-lib
  11764a11765,11766
  > .types.defaultFunctor
  > .types.defaultTypeMerge
  11774a11777,11778
  > .types.isOptionType
  > .types.isType
  11781a11786
  > .types.mkOptionType
  11788a11794
  > .types.setType
  11795a11802
  > .types.types

This means that this commit _adds_ to the API, however I can't find a
way to fix these last remaining discrepancies. At least none are
_removed_.

Test script (run with nix-repl in the PATH):

  #!/bin/sh

  set -eux

  repl() {
      suff=${1:-}
      echo "(import ./lib)$suff" \
          | nix-repl 2>&1
  }

  attrs_to_check() {
      repl "${1:-}" \
          | tr ';'  $'\n' \
          | grep "\.\.\." \
          | cut -d' ' -f2 \
          | sed -e "s/^/${1:-}./" \
          | sort
  }

  summ() {
      repl "${1:-}" \
          | tr ' ' $'\n' \
          | sort \
          | uniq
  }

  deep_summ() {
      suff="${1:-}"
      depth="${2:-4}"
      depth=$((depth - 1))
      summ "$suff"

      for attr in $(attrs_to_check "$suff" | grep -v "types.types"); do
          if [ $depth -eq 0 ]; then
              summ "$attr" | sed -e "s/^/$attr./"
          else
              deep_summ "$attr" "$depth" | sed -e "s/^/$attr./"
          fi
      done
  }

  (
      cd nixpkgs

      #git add .
      #git commit -m "Auto-commit, sorry" || true
      git checkout fixed-lib
      deep_summ > ../fixed-lib
      git checkout master
      deep_summ > ../master
  )

  if diff master fixed-lib; then
      echo "SHALLOW MATCH!"
  fi

  (
      cd nixpkgs
      git checkout fixed-lib
      repl .types
  )
2017-09-16 21:36:43 -04:00
Shea Levy d1afc718f8 Add haskellPathsInDir lib function 2017-05-01 10:49:59 -04:00
Shea Levy 56e71f62dc Add locateDominatingFile lib function 2017-03-04 13:15:23 -05:00