This is a template for a NixOS system managed with a flake. It also includes home-manager and a sample overlay.
-
Install NixOS the conventional way
-
Clone this repo wherever:
git clone https://github.com/viperML/nixos-flakes ~/nixfiles cd ~/nixfiles
-
Enter the pre-configured nix shell, that enables flake support:
nix-shell
-
Follow the instructions that will be printed to the console
legacyPackages
output. By exposing the nixpkgs that has been imported and configured, you can build packages affected by your overlay just by usingnix build .#my-package
ornix-build -A my-package
(thanks to flake-compat).NIX_PATH
set to thelegacyPackages
output. For the same reasoning, the system will use a uniformnixpkgs
configuration, so you don't have to edit~/.config/nixpkgs/config.nix
. It also allows you to run unfree packages with nix flake commands, such asnix run pkgs#discord
.- Sample overlay in
./overlay/default.nix
. An overlay allows you declare modifications to packages, in a similar fashion to Gentoo's patching.
Note: adding any new file requires that it is git tracked $ git add -N .
Either modify any file in nixos-modules
, or create a new one and add it to flake.nix
.
You can find configuration options in search.nixos.org/options.
{
config,
pkgs,
...
}:
{
option = "value";
}
Either modify any file in hm-modules
, or create a new one and add it to nixos-modules/home-manager.nix
.
{
config,
pkgs,
...
}:
{
option = "value";
}
You can find configuration options in rycee.gitlab.io/home-manager/options.html.
Everything installed by this flake is immutable, but many times you will want something to be editable. You can set a systemd-tmpfile rule to link config files from this flake to their place, such as:
# hm-modules/vscode.nix
{
config,
pkgs,
...
}:
{
systemd.user.tmpfiles.rules = [
"L+ /home/USER/.config/Code/User/settings.json - - - - /home/USER/nixfiles/config/vscode/settings.json"
];
}
In nix, you can patch any package to change its source code, or change any phase in the compile+install process. These "overrides" can be set at any place, but an overlay is a file that can hold many overrides, and propagate them to your whole flake.
# overlay/default.nix
final: prev: {
# Change the source code of awesomewm to the latest git commit, at the time of writing
awesome = prev.awesome.overrideAttrs (prevAttrs: {
version = "unstable-2022-03-06";
src = prev.fetchFromGitHub {
owner = "awesomeWM";
repo = "awesome";
rev = "392dbc21ab6bae98c5bab8db17b7fa7495b1e6a5";
sha256 = "093zapjm1z33sr7rp895kplw91qb8lq74qwc0x1ljz28xfsbp496";
};
});
}
nix-env
command as a whole. If you previously installed stuff with it, you can remove everything with:To "install" something temporall, use$ nix-env -e '.*'
nix shell pkgs#packageName
.nix-channel
. This command pre-dates flakes, and serves the purpose of manipulating yourNIX_PATH
environment variable to setnixpkgs
. With this config, we setupNIX_PATH
to the nixpkgs defined in our flake, so this tool is rendered useless.- Configure your nixpkgs in
flake.nix
(at legacyPackages). Avoid the NixOS / home-manager optionnixpkgs.config
or dropping files in~/.config/nixpkgs/config.nix
.