In NixOS, overlays allow you to customize and extend the package collection (Nixpkgs) with your own modifications. If you want to use an older version of a package, you can create an overlay that overrides the package definition with a specific version.
Here’s an example of how to create an overlay to use an older version of a package. In this case, let’s say you want to use an older version of hello, a simple test package.
Step 1: Identify the Older Version
First, you need to find the commit or the version of Nixpkgs that contains the older version of the package you want. You can do this by searching through the Nixpkgs repository on GitHub or by using nixpkgs-search for different versions.
Step 2: Create an Overlay
You can create an overlay in your NixOS configuration file (usually /etc/nixos/configuration.nix). Below is an example of how to specify an overlay to use an older version of the hello package.
{ config, pkgs, ... }:
let
# Define an overlay to override the hello package
myOverlays = self: super: {
hello = super.hello.overrideAttrs (old: {
version = "2.10"; # Replace this with the desired version
src = super.fetchFromGitHub {
owner = "gnu";
repo = "hello";
rev = "v${version}"; # Git tag of the version
sha256 = "0bawiszf75m9xhs596s0rwhzgsd4s2vwn6r00hm37jnp5gk4agb4"; # sha256 hash of the source
};
});
};
in
{
# Include the overlays
nixpkgs.overlays = [ myOverlays ];
# Now you can use the overridden package normally
environment.systemPackages = with pkgs; [
hello # This will use your older version of hello
];
}
Step 3: Update configuration.nix
Make sure you update your configuration.nix with the above code, modifying the version and sha256 according to the older version of the package you want to use. You’ll need to compute the correct sha256 hash for the specific version of the source code, which can be done using:
nix-prefetch-url <url-to-the-source>
Step 4: Rebuild your NixOS Configuration
After you have made the changes, you need to apply them by rebuilding your NixOS configuration:
sudo nixos-rebuild switch
Notes
- Ensure that the version and
sha256 correspond correctly to the older version of the package you’re trying to use.
- If you have multiple packages you want to override, you can add more definitions within the overlay.
- If the older version has different dependencies or build inputs, you may need to account for those in your override.
This process should work smoothly for most packages within Nixpkgs, leveraging the power of Nix's functional package management!