How do you reference external resources?#
In flake.nix, you can define various external sources in the inputs section.
1. Common URL types
- GitHub/GitLab:
github:user/repo/rev - Local path:
path:../other-flake - Tarball:
https://example.com/archive.tar.gz
Practical examples: referencing someone else’s Nix configuration#
1. Referencing Hyprland’s official Flake
Hyprland evolves very quickly, and the official NixOS repository may not be recent enough. You can reference its Flake directly:
inputs.hyprland.url = "github:hyprwm/Hyprland";
outputs = { self, nixpkgs, hyprland, ... }: {
nixosConfigurations.my-pc = nixpkgs.lib.nixosSystem {
modules = [
./configuration.nix
{ programs.hyprland = {
enable = true;
package = hyprland.packages.x86_64-linux.hyprland;
portalPackage = hyprland.packages.x86_64-linux.xdg-desktop-portal-hyprland;
}; } # 官方文档的包与 portal 配对方式
];
};
};2. Referencing someone else’s Neovim configuration
Suppose someone has packaged their Neovim setup as a Flake:
inputs.my-neovim.url = "github:someone/neovim-flake";
# 在 configuration.nix 里:
environment.systemPackages = [ inputs.my-neovim.packages.x86_64-linux.default ];🔄 Collaboration and dependency conflicts#
When you reference multiple Flakes, each may depend on a different version of nixpkgs. Check the external projects’ compatibility requirements before sharing a version.
Solve it with follows!
If the external Flake supports your current nixpkgs, you can make a specified input follow the main configuration’s version:
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs"; # 强制跟随主 nixpkgs
};
};Collaboration workflow#
- Add an Input: edit
flake.nix. - Lock versions: run
nix flake lock(which updatesflake.lockautomatically). - Use an Output: reference the corresponding output by variable name in your code.
Summary#
Learning to reference external Flakes puts the whole Nix community’s toolkit at your disposal. Desktop environments, editors, and specialized development tools can all be made to work “out of the box.”
References & further reading
Adapted from technical notes. Examples use generic names.
Continue along a reading path
Flakes: project and dependency management
- 01Flake structure: inputs and outputs
- 02flake.lock and reproducibility
- 03Flake Utils and project templates
- 04External Flake references and collaboration
- 05Migrating NixOS configuration to Flakes
- 06A tutorial on Flake projects and dependency management
- 07How Flake updates work
- 08Rolling back and manually editing flake.lock