Common patterns
Getting a recent version of a package from nixpkgs-unstable
By default, devenv uses a fork of nixpkgs with additional fixes. This fork can be several months behind nixpkgs-unstable. You can still get a more recently updated package from nixpkgs-unstable into your devenv.
- Add nixpkgs-unstableinput todevenv.yaml:
inputs:
  nixpkgs:
    url: github:cachix/devenv-nixpkgs/rolling
  nixpkgs-unstable:
    url: github:nixos/nixpkgs/nixpkgs-unstable
- Use the package in your devenv.nix:
{ pkgs, inputs, ... }:
let
  pkgs-unstable = import inputs.nixpkgs-unstable { system = pkgs.stdenv.system; };
in
{
  packages = [
    pkgs-unstable.elmPackages.elm-test-rs
  ];
}
Nix patterns
Add a directory to $PATH
This example adds Elixir install scripts to ~/.mix/escripts:
{ ... }:
{
  languages.elixir.enable = true;
  enterShell = ''
    export PATH="$HOME/.mix/escripts:$PATH"
  '';
}
Escape Nix curly braces inside shell scripts
Container patterns
Exclude packages from a container
{ pkgs, ... }: {
  packages = [
    pkgs.git
  ] ++ lib.optionals (!config.container.isBuilding) [
    pkgs.haskell-language-server
  ];
}
Cross-platform patterns
Configure the shell based on the current machine
Some packages are available only on certain processor architectures or operating systems.
A number of helper functions exist in pkgs.stdenv to help you dynamically configure the shell based on the current machine.
A few of the most commonly used functions are:
- stdenv.isLinuxto target machines running Linux
- 
stdenv.isDarwinto target machines running macOS
- 
stdenv.isAarch64to target ARM64 processors
- stdenv.isx86_64to target X86_64 processors
{ pkgs, lib, ... }: {
  packages = [
    pkgs.ncdu
  ] ++ lib.optionals pkgs.stdenv.isLinux [
    pkgs.inotify-tools
  ] ++ lib.optionals pkgs.stdenv.isDarwin [
    pkgs.darwin.apple_sdk.frameworks.Security
  ];
}
macOS patterns
Link against macOS system frameworks
When compiling for macOS, you may need to link against system frameworks, like CoreFoundation.
These frameworks can be found in pkgs.darwin.apple_sdk.frameworks.
Add the frameworks you need to packages and Nix will configure the shell with necessary linker flags.
{ pkgs, lib, ... }:
{
  packages = [
    # Other dependencies
  ] ++ lib.optionals pkgs.stdenv.isDarwin [
    pkgs.darwin.apple_sdk.frameworks.CoreFoundation
    pkgs.darwin.apple_sdk.frameworks.Security
    pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
  ];
}
Run x86 binaries on ARM Macs via Rosetta
It's possible to tell Nix to use Intel packages on macOS machines running on ARM.