In Devbox 0.4.7, we added support for installing packages from Nix flakes. For power users of Nix, this provides more flexibility and customization for your Devbox project. Using the power of flakes, developers can now create their own packages, modify nixpkgs, or install packages from sources outside of the Nix store.
In this post, we'll demonstrate how to use a Nix flake to modify a package from the Nixpkg repository and then use that modified package in our Devbox project. We'll be using the
overlay example from our Devbox repo. What is a Flake? A flake is a simplified way to package software with Nix that lets you declaratively define dependencies and build instructions. Unlike most Nix files or derivations, flakes have a strict schema for representing your packages and outputs. The high-level schema looks something like this: { description = "This flake outputs a modified version of Yarn that uses NodeJS 16" inputs = {} # A set of the dependencies our flake will use outputs = {}: {} # A function that turns our dependencies into packages, apps, and other outputs. } Let's go through each part in detail, to show how we will build our modified yarn package. Inputs inputs = { nixpkgs.url = "nixpkgs/nixos-21.11"; flake-utils.url = "github:numtide/flake-utils"; }; Our flake is going to use two inputs to create our packages. We'll define our input by mapping a name to a flake reference that tells Nix where to fetch it: Nixpkgs is the default repository of Packages for Nix. We define an input name, nixpkgs, and set its URL to "nixpkgs/nixos-21.11" Flake-utils is a set of functions that make developing Nix Flakes easier. We define a name flake-utils and set its reference to a Github-hosted flake using "github:numtide/flake-utils" Whenever we build the outputs for our Flake, Nix will pull these inputs from their URL and pin them in a flake.lock file. This file ensures that we will use the same sources every time we build or run the Flake. With these inputs defined, we can use them in our outputs closure to create the packages we want. Further Reading Nix Manual entry on Flake Inputs Outputs: A flake's output is a function that takes a set of inputs, and returns an attribute set of packages, apps, templates, and other outputs that users can reference. In this case, the input set consists of the following: { self, # A reference to the Flake itself nixpkgs, #Our nixpkgs input defined in the previous section flake-utils, #The…