|
| 1 | +# Overlays |
| 2 | + |
| 3 | +!!! info "New in version 1.4.2" |
| 4 | + |
| 5 | +Overlays in devenv allow you to modify or extend the default package set (`pkgs`) that devenv uses. This is useful when you need to: |
| 6 | + |
| 7 | +- Override existing packages to apply patches |
| 8 | +- Add new packages that aren't in the default set |
| 9 | +- Use custom builds of existing packages |
| 10 | + |
| 11 | +## Using overlays |
| 12 | + |
| 13 | +To add overlays to your devenv configuration, use the `overlays` option in your `devenv.nix` file: |
| 14 | + |
| 15 | +```nix |
| 16 | +{ pkgs, ... }: |
| 17 | +
|
| 18 | +{ |
| 19 | + # List of overlays to apply to pkgs |
| 20 | + overlays = [ |
| 21 | + # Each overlay is a function that takes two arguments: final and prev |
| 22 | + (final: prev: { |
| 23 | + # Override an existing package |
| 24 | + hello = prev.hello.overrideAttrs (oldAttrs: { |
| 25 | + patches = (oldAttrs.patches or []) ++ [ ./hello-fix.patch ]; |
| 26 | + }); |
| 27 | +
|
| 28 | + # Add a custom package |
| 29 | + my-custom-package = final.callPackage ./my-package.nix {}; |
| 30 | + }) |
| 31 | + ]; |
| 32 | +
|
| 33 | + # Now you can use the modified or added packages |
| 34 | + packages = [ pkgs.hello pkgs.my-custom-package ]; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## How overlays work |
| 39 | + |
| 40 | +Each overlay is a function that takes two arguments: |
| 41 | +- `final`: The final package set after all overlays are applied |
| 42 | +- `prev`: The package set as it existed before this overlay (but after previous overlays) |
| 43 | + |
| 44 | +The function should return an attrset containing the packages you want to add or modify. These will be merged into the final package set. |
| 45 | + |
| 46 | +## Common use cases |
| 47 | + |
| 48 | +### Patching existing packages |
| 49 | + |
| 50 | +```nix |
| 51 | +overlays = [ |
| 52 | + (final: prev: { |
| 53 | + # Apply a patch to fix a bug |
| 54 | + somePackage = prev.somePackage.overrideAttrs (oldAttrs: { |
| 55 | + patches = (oldAttrs.patches or []) ++ [ ./my-fix.patch ]; |
| 56 | + }); |
| 57 | + }) |
| 58 | +]; |
| 59 | +``` |
| 60 | + |
| 61 | +### Using a different version of a package |
| 62 | + |
| 63 | +```nix |
| 64 | +overlays = [ |
| 65 | + (final: prev: { |
| 66 | + # Use a specific version of Node.js |
| 67 | + nodejs = prev.nodejs-18_x; |
| 68 | + }) |
| 69 | +]; |
| 70 | +``` |
| 71 | + |
| 72 | +### Adding custom packages |
| 73 | + |
| 74 | +```nix |
| 75 | +overlays = [ |
| 76 | + (final: prev: { |
| 77 | + # Add a package from a local derivation |
| 78 | + my-tool = final.callPackage ./nix/my-tool.nix {}; |
| 79 | + }) |
| 80 | +]; |
| 81 | +``` |
| 82 | + |
| 83 | +### Using packages from older nixpkgs |
| 84 | + |
| 85 | +First, add the extra input to your `devenv.yaml`: |
| 86 | + |
| 87 | +```yaml |
| 88 | +inputs: |
| 89 | + nixpkgs: |
| 90 | + url: github:cachix/devenv-nixpkgs/rolling |
| 91 | + nixpkgs-unstable: |
| 92 | + url: github:nixos/nixpkgs/nixpkgs-unstable |
| 93 | +``` |
| 94 | +
|
| 95 | +Then use it in your `devenv.nix`: |
| 96 | + |
| 97 | +```nix |
| 98 | +{ pkgs, inputs, ... }: |
| 99 | +
|
| 100 | +{ |
| 101 | + overlays = [ |
| 102 | + (final: prev: { |
| 103 | + # Use a package from nixpkgs-unstable |
| 104 | + nodejs = (import inputs.nixpkgs-unstable { |
| 105 | + system = prev.stdenv.system; |
| 106 | + }).nodejs; |
| 107 | + }) |
| 108 | + ]; |
| 109 | +
|
| 110 | + # Now you can use these packages from your regular pkgs |
| 111 | + languages.javascript.enable = true; |
| 112 | +} |
| 113 | +``` |
0 commit comments