In NixOS, how do I create an overlay that uses a package from my local nixpkgs instead of from the remote repository?
Assistant
To create an overlay in NixOS that uses a package from your local nixpkgs instead of fetching it from a remote repository, you can follow these steps:
Step 1: Clone or Create Your Local nixpkgs
First, make sure you have a local copy of the nixpkgs repository. You can clone it if you haven't already:
bash
User
Can I make it in my configuration.nix file instead of two files?
Assistant
Yes, you can define your overlay directly in your configuration.nix file in NixOS without the need for creating separate overlay files. This is a more straightforward approach if you're managing your configuration in a single file. Here’s how you can do it:
Create a directory for your overlays if you don't have one already. A common practice is to create the overlays directory inside your own configuration.
bash
mkdir -p ~/.config/nixpkgs/overlays
Step 3: Write the Overlay
Create a new file in the overlays directory, e.g., my-overlay.nix:
nix
# ~/.config/nixpkgs/overlays/my-overlay.nix
self: super: {
my-package = super.my-package.override {
# Replace this with your local package from your local nixpkgs
inherit (import ~/nixpkgs) my-package;
};
}
In the above my-overlay.nix, replace my-package with the actual name of the package you want to override.
Step 4: Configure Nix to Use the Overlay
You need to tell Nix to use your overlays. You can do this by configuring ~/.config/nixpkgs/overlays/ in your nix.conf file or similar configuration file. Here's how to do that:
You can either create or edit ~/.config/nixpkgs/overlays.nix:
Now you can build your package using Nix, and it will use the version from your local nixpkgs instead of the remote one.
For example, if you want to build my-package, run:
bash
nix build nixpkgs#my-package
Note:
Make sure any paths you use are correct and that ~/nixpkgs points to the right location where you have your nixpkgs checked out.
If you make changes to the nixpkgs path (like updating your local repository), they should be reflected when you build the package using the overlay.
That's it! You’ve successfully created an overlay in NixOS that uses a package from your local nixpkgs.
Open your configuration.nix file, which is usually located at /etc/nixos/configuration.nix. Then, add your overlay directly in the nixpkgs.overlays section.
Here’s an example of how to do it:
nix
{ pkgs, ... }:
{
# Your other configuration options here...
nixpkgs.overlays = [
(self: super: {
# Replace 'my-package' with the actual package name you want to use
my-package = super.my-package.override {
# Use the package from your local nixpkgs repository
inherit (import /path/to/your/local/nixpkgs) my-package;
};
})
];
}
Step 2: Replace the Path
Make sure to replace /path/to/your/local/nixpkgs with the actual path to your local nixpkgs repository. For example, if your local repo is located at ~/nixpkgs, you might reference it like this:
By integrating the overlay directly into your configuration.nix, you streamline your configuration management process by keeping everything in one file. This method allows you to use your local packages directly without additional files. Remember to run nixos-rebuild switch after making changes for them to take immediate effect!