All Projects → numtide → nix-filter

numtide / nix-filter

Licence: MIT license
a small self-container source filtering lib

Programming Languages

Nix
1067 projects
shell
77523 projects

Labels

Projects that are alternatives of or similar to nix-filter

denxi
Denxi is a programming model for distributing data. It reduces the cost of producing package managers, storefronts, operating systems, and CI/CD systems.
Stars: ✭ 65 (-4.41%)
Mutual labels:  nix
emacs
Nightly custom Emacs builds for macOS Nix environments
Stars: ✭ 25 (-63.24%)
Mutual labels:  nix
triton
Triton Operating System
Stars: ✭ 56 (-17.65%)
Mutual labels:  nix
nixdots
I have no idea what the hell I'm doing
Stars: ✭ 46 (-32.35%)
Mutual labels:  nix
flake-nimble
Nimble packages Nix flake [maintainer=@ehmry]
Stars: ✭ 26 (-61.76%)
Mutual labels:  nix
easy-hls-nix
Easy Haskell Language Server tooling with Nix!
Stars: ✭ 56 (-17.65%)
Mutual labels:  nix
dotfiles
🏠
Stars: ✭ 53 (-22.06%)
Mutual labels:  nix
nyx
⚙️Nix[OS] Configuration
Stars: ✭ 50 (-26.47%)
Mutual labels:  nix
supernova
🌌 Apache Pulsar client for Haskell
Stars: ✭ 35 (-48.53%)
Mutual labels:  nix
deadnix
Scan Nix files for dead code
Stars: ✭ 121 (+77.94%)
Mutual labels:  nix
base16.nix
Quickly theme programs in your favourite base16 colorscheme
Stars: ✭ 61 (-10.29%)
Mutual labels:  nix
nix-rice
A library to functionally define your configuration and theme (rice) with Nix
Stars: ✭ 43 (-36.76%)
Mutual labels:  nix
osu.nix
DEPRECATED osu! on Nix
Stars: ✭ 13 (-80.88%)
Mutual labels:  nix
dotfiles
My dotfiles for Bash/Zsh, Vim/Neovim, Doom Emacs, tmux, Git, terminal emulators, JupyterLab, aria2, mpv, Nix and Homebrew
Stars: ✭ 149 (+119.12%)
Mutual labels:  nix
nix-bisect
Bisect nix builds. Status: alpha/proof of concept. You'll probably have to dig into the implementation if you want to use it. Built for personal use, lightly maintained. PRs welcome. Issues welcome, but I make no promises regarding responses or fix
Stars: ✭ 72 (+5.88%)
Mutual labels:  nix
idris2-pkgs
An unofficial Idris2 package repository for Nix
Stars: ✭ 32 (-52.94%)
Mutual labels:  nix
nixos-tutorial
one hour, hands-on
Stars: ✭ 118 (+73.53%)
Mutual labels:  nix
haskell-template
Haskell project template using Nix + Flakes + VSCode (HLS)
Stars: ✭ 68 (+0%)
Mutual labels:  nix
homeage
runtime decrypted age secrets for nix home manager
Stars: ✭ 43 (-36.76%)
Mutual labels:  nix
nix-npm-buildpackage
Build nix packages that use npm/yarn
Stars: ✭ 48 (-29.41%)
Mutual labels:  nix

logo
nix-filter - a small self-contained source filtering lib

STATUS: beta

When using nix within a project, developers often use src = ./.; for a project like this:

{ stdenv }:
stdenv.mkDerivation {
  name = "my-project";
  src = ./.;
}

This works but has an issue; on each build, nix will copy the whole project source code into the /nix/store. Including the .git folder and any temporary files left over by the editor.

The main workaround is to use either builtins.fetchGit ./. or one of the many gitignore filter projects but this is not precise enough. If the project README changes, it should not rebuild the project. If the nix code changes, it should not rebuild the project.

This project is a small library that makes it easy to filter in and out what files should go into a nix derivation.

Example usage

nix-filter works best for projects where the files needed for a build are easy to match. Using it with only an exclude argument will likely not reduce the reasons for rebuilds by a lot. Here's an Example:

{ stdenv, nix-filter }:
stdenv.mkDerivation {
  name = "my-project";
  src = nix-filter {
    root = ./.;
    # If no include is passed, it will include all the paths.
    include = [
      # Include the "src" path relative to the root.
      "src"
      # Include this specific path. The path must be under the root.
      ./package.json
      # Include all files with the .js extension
      (nix-filter.matchExt "js")
    ];

    # Works like include, but the reverse.
    exclude = [
      ./main.js
    ];
  };
}

Usage

Import this folder. Eg:

let
  nix-filter = import ./path/to/nix-filter;
in
 # ...

The top-level is a functor that takes:

  • path of type path: pointing to the root of the source to add to the /nix/store.
  • name of type string (optional): the name of the derivation (defaults to "source")
  • include of type list(string|path|matcher) (optional): a list of patterns to include (defaults to all).
  • exclude of type list(string|path|matcher) (optional): a list of patterns to exclude (defaults to none).

The include and exclude take a matcher, and automatically convert the string and path types to a matcher.

The matcher is a function that takes a path and type and returns true if the pattern matches.

Builtin matchers

The functor also contains a number of matchers:

  • nix-filter.matchExt: ext -> returns a function that matches the given file extension.
  • nix-filter.inDirectory: directory -> returns a function that matches a directory and any path inside of it.
  • nix-filter.isDirectory: matches all paths that are directories

Combining matchers

  • and: a -> b -> c combines the result of two matchers into a new matcher.
  • or_: a -> b -> c combines the result of two matchers into a new matcher.

NOTE: or is a keyword in nix, which is why we use a variation here.

REMINDER: both, include & exlude already XOR elements, so or_ is not useful at the top level.

Design notes

This solution uses builtins.path { path, name, filter ? path: type: true } under the hood, which ships with Nix.

While traversing the filesystem, starting from path, it will call filter on each file and folder recursively. If the filter returns false then the file or folder is ignored. If a folder is ignored, it won't recurse into it anymore.

Because of that, it makes it difficult to implement recursive glob matchers. Something like **/*.js would necessarily have to add every folder, to be able to traverse them. And those empty folders will end-up in the output.

If we want to control rebuild, it's important to have a fixed set of folders.

One possibility is to use a two-pass system, where first all the folders are being added, and then the empty ones are being filtered out. But all of this happens at Nix evaluation time. Nix evaluation is already slow enough like that.

That's why nix-filter is asking the users to explicitly list all the folders that they want to include, and using only an exclude is not recommended.

Future development

Add more matchers.

Related projects

License

Copyright (c) 2021 Numtide under the MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].