In the context of the Nix ecosystem, flake.nix and serve different purposes. To briefly summarize, a is a newer concept that defines inputs and outputs for reproducible Nix builds, making it easier to manage dependencies. A , on the other hand, is typically used to create a reproducible development environment.
User
yeah but i still want a shell.nix to use with direnv
Assistant
If you're using direnv and you'd like to continue using a shell.nix file alongside a more modern flake.nix setup, you need to create a shell.nix that invokes the development shell environment from the flake configuration. Here's how you can create a shell.nix file to work with your flake.nix:
nix
shell.nix
flake.nix
shell.nix
To create a shell.nix from a flake.nix, you actually don't create a physical shell.nix file. Instead, you define a devShell within your flake.nix which serves the same purpose.
Here's how you can define a devShell in a flake.nix:
nix
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... } @ inputs:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
config = { allowUnfree = true; };
};
in
{
# The application's default package.
packages.default = pkgs.hello;
# Your development environment (equivalent to a `shell.nix`)
devShell = pkgs.mkShell {
buildInputs = [ pkgs.hello ];
# Add any environment setup here, like `export VAR=val` or shell aliases.
};
}
);
}
In this example, devShell is defined within the flake.nix which contains the dependencies for your development environment (in this case, the hello package).
To enter this devShell, you don't call nix-shell with shell.nix anymore. You issue the command:
bash
nix develop
Or, if you're not actually in a flake directory but want to enter the development shell of an arbitrary flake:
bash
nix develop /path/to/your/flake
Using Nix flakes, this command provides you with the environment defined in devShell. If you are migrating from a non-flake structure and have an existing shell.nix, the buildInputs and other configuration should resemble what you already have in your shell.nix. Adapt them as needed within the devShell attribute of your flake.nix file.
Please note that in order to use flakes, you will need to have Nix with flakes support enabled on your machine (Nix version 2.4 or later). Flakes are still experimental at the time when my knowledge was last updated, so ensure your Nix is up-to-date and that you've enabled experimental features if necessary.
# shell.nix
{ pkgs ? import <nixpkgs> { } }:
let
flake = builtins.getFlake (toString ./.);
in
pkgs.mkShell {
# Pulls in the devShell from flake.nix for the current system.
buildInputs = with flake.inputs.nixpkgs.legacyPackages.${flake.system}; [
(flake.devShell.${flake.system} or null)
];
# Add additional shell hooks or environment variables here if needed.
}
This shell.nix script uses a built-in Nix function builtins.getFlake to grab the current flake configuration, allowing you to extract the devShell for the current system. You then use the pkgs.mkShell function to create the environment. The buildInputs line effectively extracts the development shell environment you've already defined in your flake.nix.
To make direnv work with this shell.nix, you could use nix-direnv, an integration of direnv with Nix that has flake support. With nix-direnv configured, you can have a .envrc file with a command to use the provided shell.nix:
bash
# .envrc
use nix
Setting up nix-direnv typically involves adding it to your user's Nix profile or configuration, and it then automatically integrates with direnv. Ensure you have direnv hooked into your shell and that you've allowed it to load the configuration for your project directory (direnv allow`).
By using nix-direnv, whenever you switch to the project's directory, direnv will load the environment specified in the shell.nix, which, in turn, uses the environment from the flake.nix.
Don't forget to restart your shell or run direnv allow after making changes to .envrc or shell.nix files for the changes to take effect.