nixos-config/flake.nix

528 lines
20 KiB
Nix
Raw Normal View History

{
2022-11-25 06:35:11 +00:00
description = "foo-dogsquared's abomination of a NixOS configuration";
2022-02-04 12:48:02 +00:00
nixConfig = {
extra-substituters =
"https://nix-community.cachix.org https://foo-dogsquared.cachix.org";
extra-trusted-public-keys =
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= foo-dogsquared.cachix.org-1:/2fmqn/gLGvCs5EDeQmqwtus02TUmGy0ZlAEXqRE70E=";
2022-02-04 12:48:02 +00:00
};
inputs = {
# I know NixOS can be stable but we're going cutting edge, baybee! While
# `nixpkgs-unstable` branch could be faster delivering updates, it is
# looser when it comes to stability for the entirety of this configuration.
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# Here are the nixpkgs variants used for creating the system configuration
# in `mkHost`.
nixos-stable.url = "github:NixOS/nixpkgs/nixos-23.05";
nixos-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
nixos-unstable-small.url = "github:NixOS/nixpkgs/nixos-unstable-small";
2021-12-06 10:12:00 +00:00
2022-11-25 06:35:11 +00:00
# We're using these libraries for other functions.
2021-12-18 09:41:45 +00:00
flake-utils.url = "github:numtide/flake-utils";
# Managing home configurations.
home-manager.url = "github:nix-community/home-manager";
# This is what AUR strives to be.
nur.url = "github:nix-community/NUR";
2022-02-04 15:36:22 +00:00
# Generate your NixOS systems to various formats!
nixos-generators.url = "github:nix-community/nixos-generators";
nixos-generators.inputs.nixpkgs.follows = "nixpkgs";
# Managing your secrets.
sops-nix.url = "github:Mic92/sops-nix";
sops-nix.inputs.nixpkgs.follows = "nixpkgs";
2021-12-18 09:41:45 +00:00
# Easy access to development environments.
devshell.url = "github:numtide/devshell";
devshell.inputs.nixpkgs.follows = "nixpkgs";
2021-12-18 09:41:45 +00:00
2022-11-25 06:35:11 +00:00
# We're getting more unstable there should be a black hole at my home right
# now. Also, we're seem to be collecting text editors like it is Pokemon.
neovim-nightly-overlay.url = "github:nix-community/neovim-nightly-overlay";
neovim-nightly-overlay.inputs.nixpkgs.follows = "nixpkgs";
2022-01-02 00:40:01 +00:00
emacs-overlay.url = "github:nix-community/emacs-overlay";
emacs-overlay.inputs.nixpkgs.follows = "nixpkgs";
2022-01-02 00:40:01 +00:00
2022-08-04 13:32:15 +00:00
helix-editor.url = "github:helix-editor/helix";
helix-editor.inputs.nixpkgs.follows = "nixpkgs";
# Guix in NixOS?!
guix-overlay.url = "github:foo-dogsquared/nix-overlay-guix";
flake.lock: update Flake lock file updates: • Updated input 'agenix': 'github:ryantm/agenix/a17d1f30550260f8b45764ddbd0391f4b1ed714a' (2022-02-02) → 'github:ryantm/agenix/b4ab630f195cb15f833cb285de232b1a22d1ea0a' (2022-02-22) • Updated input 'emacs-overlay': 'github:nix-community/emacs-overlay/6ecfaeb355326456e7b331d1527105adab360adc' (2022-02-18) → 'github:nix-community/emacs-overlay/f7eca8816dd24bed25f7c6a49cf4185d3725642d' (2022-02-27) • Updated input 'home-manager': 'github:nix-community/home-manager/4f4165a8b9108818ab0193bbd1a252106870b2a2' (2022-02-17) → 'github:nix-community/home-manager/2499b916921adde8a694117bc007efdde8bbd918' (2022-02-27) • Updated input 'neovim-nightly-overlay': 'github:nix-community/neovim-nightly-overlay/7ab23810d3844251fef656d7acc4bfbb2c4584bd' (2022-02-18) → 'github:nix-community/neovim-nightly-overlay/9a5300fb01ef2eb5013f932913e8e7d5d96e4e9d' (2022-02-26) • Updated input 'neovim-nightly-overlay/neovim-flake': 'github:neovim/neovim/faeff49cbfd190afba67e221412b7132b7ad8360?dir=contrib' (2022-02-18) → 'github:neovim/neovim/005a7aa1670841dfdffb7559161a7d37d6c14bc4?dir=contrib' (2022-02-25) • Updated input 'neovim-nightly-overlay/nixpkgs': 'github:nixos/nixpkgs/48d63e924a2666baf37f4f14a18f19347fbd54a2' (2022-02-10) → 'github:nixos/nixpkgs/7f9b6e2babf232412682c09e57ed666d8f84ac2d' (2022-02-21) • Updated input 'nixos-generators': 'github:nix-community/nixos-generators/296067b9c7a172d294831dec89d86847f30a7cfc' (2021-12-26) → 'github:nix-community/nixos-generators/05773c5d1584cebd5eda41675c3456dcabdef42a' (2022-02-25) • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/19574af0af3ffaf7c9e359744ed32556f34536bd' (2022-02-16) → 'github:NixOS/nixpkgs/7dda625e7f63445ff61bcf11618999d2c97d5865' (2022-02-23) • Updated input 'nur': 'github:nix-community/NUR/8d98d367039695d78ef5cf89da6e577886de81c1' (2022-02-18) → 'github:nix-community/NUR/1dfd799af1c96ce96e7db9c4f97d8a9fc4f0e30d' (2022-02-27) • Updated input 'rust-overlay': 'github:oxalica/rust-overlay/9ce263da4310d02bd16f18f4db1c617265939a3e' (2022-02-19) → 'github:oxalica/rust-overlay/7f273929e83a196f96a0dbee9ea565952e340bd6' (2022-02-26)
2022-02-27 02:07:18 +00:00
# Generating an entire flavored themes with Nix?
nix-colors.url = "github:misterio77/nix-colors";
# Removing the manual partitioning part with a little boogie.
disko.url = "github:nix-community/disko";
disko.inputs.nixpkgs.follows = "nixpkgs";
# Deploying stuff with Nix. This is becoming a monorepo for everything I
# need and I'm liking it.
deploy.url = "github:serokell/deploy-rs";
deploy.inputs.nixpkgs.follows = "nixpkgs";
# Someone has already solved downloading Firefox addons so we'll use it.
firefox-addons.url = "gitlab:rycee/nur-expressions?dir=pkgs/firefox-addons";
firefox-addons.inputs.nixpkgs.follows = "nixpkgs";
};
2022-01-07 03:09:32 +00:00
outputs = inputs@{ self, nixpkgs, ... }:
let
# A set of images with their metadata that is usually built for usual
# purposes. The format used here is whatever formats nixos-generators
# support.
images = lib'.importTOML ./images.toml;
# A set of users with their metadata to be deployed with home-manager.
users = lib'.importTOML ./users.toml;
# A set of image-related utilities for the flake outputs.
inherit (import ./lib/images.nix { inherit inputs; lib = lib'; }) mkHost mkHome mkImage listImagesWithSystems;
2022-01-02 14:30:00 +00:00
# The order here is important(?).
overlays = [
# Put my custom packages to be available.
2022-08-13 00:31:09 +00:00
self.overlays.default
2022-08-04 13:32:15 +00:00
(final: prev: {
inherit (inputs.firefox-addons.lib.${defaultSystem}) buildFirefoxXpiAddon;
firefox-addons = final.callPackage ./pkgs/firefox-addons { };
})
# Neovim nightly!
inputs.neovim-nightly-overlay.overlays.default
2021-12-26 09:33:00 +00:00
2022-01-02 00:40:01 +00:00
# Emacs unstable version!
inputs.emacs-overlay.overlays.default
2022-01-02 00:40:01 +00:00
# Access to NUR.
inputs.nur.overlay
];
2023-07-30 06:16:40 +00:00
defaultSystem = "x86_64-linux";
2022-11-25 06:35:11 +00:00
# Just add systems here and it should add systems to the outputs.
2023-07-30 06:16:40 +00:00
systems = with inputs.flake-utils.lib.system; [
"x86_64-linux"
"aarch64-linux"
"riscv64-linux"
];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
extraArgs = {
inherit (inputs) nix-colors;
inherit inputs;
# This is a variable that is used to check whether the module is
# exported or not. Useful for configuring parts of the configuration
# that is otherwise that cannot be exported for others' use.
_isfoodogsquaredcustom = true;
};
# We're considering this as the variant since we'll export the custom
# library as `lib` in the output attribute.
lib' = nixpkgs.lib.extend (final: prev:
import ./lib { lib = prev; }
// import ./lib/private.nix { lib = final; });
2023-02-05 08:27:14 +00:00
# The shared configuration for the entire list of hosts for this cluster.
# Take note to only set as minimal configuration as possible since we're
# also using this with the stable version of nixpkgs.
hostSharedConfig = { config, lib, pkgs, ... }: {
2022-09-19 02:55:31 +00:00
# Some defaults for evaluating modules.
_module.check = true;
# Only use imports as minimally as possible with the absolute
# requirements of a host. On second thought, only on flakes with
# optional NixOS modules.
imports = [
inputs.home-manager.nixosModules.home-manager
inputs.nur.nixosModules.nur
inputs.sops-nix.nixosModules.sops
inputs.guix-overlay.nixosModules.guix
inputs.disko.nixosModules.disko
];
# BOOOOOOOOOOOOO! Somebody give me a tomato!
services.xserver.excludePackages = with pkgs; [ xterm ];
# Set several paths for the traditional channels.
nix.nixPath =
lib.mapAttrsToList
(name: source:
let
name' = if (name == "self") then "config" else name;
in
"${name'}=${source}")
inputs
++ [
"/nix/var/nix/profiles/per-user/root/channels"
];
# Please clean your temporary crap.
2023-04-22 06:10:03 +00:00
boot.tmp.cleanOnBoot = lib.mkDefault true;
# We live in a Unicode world and dominantly English in technical fields so we'll
# have to go with it.
i18n.defaultLocale = lib.mkDefault "en_US.UTF-8";
# The global configuration for the home-manager module.
2022-09-19 02:55:31 +00:00
home-manager.useUserPackages = lib.mkDefault true;
home-manager.useGlobalPkgs = lib.mkDefault true;
home-manager.sharedModules =
(import ./modules/home-manager {
inherit lib;
isInternal = true;
})
2022-09-29 03:35:00 +00:00
++ [ userSharedConfig ];
home-manager.extraSpecialArgs = extraArgs;
# Enabling some things for sops.
2022-09-19 02:55:31 +00:00
programs.gnupg.agent = lib.mkDefault {
enable = true;
enableSSHSupport = true;
};
2022-09-19 02:55:31 +00:00
services.openssh.enable = lib.mkDefault true;
# We're setting Guix service package with the flake-provided package.
# This is to prevent problems setting with overlays as much as I like
# using them.
services.guix.package = inputs.guix-overlay.packages.${config.nixpkgs.system}.guix;
};
# The default config for our home-manager configurations. This is also to
# be used for sharing modules among home-manager users from NixOS
# configurations with `nixpkgs.useGlobalPkgs` set to `true` so avoid
# setting nixpkgs-related options here.
userSharedConfig = { pkgs, config, lib, ... }: {
imports = [
inputs.nur.hmModules.nur
inputs.sops-nix.homeManagerModules.sops
];
# Hardcoding this is not really great especially if you consider using
# other locales but its default values are already hardcoded so what
# the hell. For other users, they would have to do set these manually.
xdg.userDirs =
let
2022-09-19 02:55:31 +00:00
# The home directory-related options should be already taken care
# of at this point. It is an ABSOLUTE MUST that it is set properly
# since other parts of the home-manager config relies on it being
# set properly.
#
2022-11-25 06:35:11 +00:00
# Here are some of the common cases for setting the home directory
# options.
2022-09-19 02:55:31 +00:00
#
2022-11-25 06:35:11 +00:00
# * For exporting home-manager configurations, this is done in this
# flake definition.
# * For NixOS configs, this is done automatically by the
# home-manager NixOS module.
2022-09-19 02:55:31 +00:00
# * Otherwise, you'll have to manually set them.
appendToHomeDir = path: "${config.home.homeDirectory}/${path}";
in
{
desktop = appendToHomeDir "Desktop";
documents = appendToHomeDir "Documents";
download = appendToHomeDir "Downloads";
music = appendToHomeDir "Music";
pictures = appendToHomeDir "Pictures";
publicShare = appendToHomeDir "Public";
templates = appendToHomeDir "Templates";
videos = appendToHomeDir "Videos";
};
2022-09-25 03:46:14 +00:00
programs.home-manager.enable = true;
manual = lib.mkDefault {
html.enable = true;
json.enable = true;
manpages.enable = true;
};
home.stateVersion = lib.mkDefault "23.11";
};
nixSettingsSharedConfig = { config, lib, pkgs, ... }: {
# I want to capture the usual flakes to its exact version so we're
# making them available to our system. This will also prevent the
# annoying downloads since it always get the latest revision.
nix.registry =
lib.mapAttrs'
(name: flake:
let
name' = if (name == "self") then "config" else name;
in
lib.nameValuePair name' { inherit flake; })
inputs;
# Parallel downloads! PARALLEL DOWNLOADS! It's like Pacman 6.0 all over
# again.
nix.package = pkgs.nixUnstable;
# Set the configurations for the package manager.
nix.settings = {
# Set several binary caches.
substituters = [
"https://nix-community.cachix.org"
"https://foo-dogsquared.cachix.org"
];
trusted-public-keys = [
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
"foo-dogsquared.cachix.org-1:/2fmqn/gLGvCs5EDeQmqwtus02TUmGy0ZlAEXqRE70E="
];
# Sane config for the package manager.
# TODO: Remove this after nix-command and flakes has been considered
# stable.
#
# Since we're using flakes to make this possible, we need it. Plus, the
# UX of Nix CLI is becoming closer to Guix's which is a nice bonus.
experimental-features = [ "nix-command" "flakes" "repl-flake" ];
auto-optimise-store = lib.mkDefault true;
};
# Stallman-senpai will be disappointed.
nixpkgs.config.allowUnfree = true;
# Extend nixpkgs with our overlays except for the NixOS-focused modules
# here.
nixpkgs.overlays = overlays;
};
2022-08-06 06:00:04 +00:00
in
{
2021-12-18 09:41:45 +00:00
# Exposes only my library with the custom functions to make it easier to
2022-07-09 06:04:17 +00:00
# include in other flakes for whatever reason may be.
lib = import ./lib { lib = nixpkgs.lib; };
2021-12-18 09:41:45 +00:00
# A list of NixOS configurations from the `./hosts` folder. It also has
# some sensible default configurations.
nixosConfigurations =
let
images' = listImagesWithSystems images;
in
lib'.mapAttrs
(_: host:
let
name = host._name;
path = ./hosts/${name};
extraModules = [
({ lib, ... }: {
config = lib.mkMerge [
{ networking.hostName = lib.mkForce name; }
(lib.mkIf (host ? domain)
{ networking.domain = lib.mkForce host.domain; })
];
})
hostSharedConfig
nixSettingsSharedConfig
path
];
in
mkHost {
inherit extraModules extraArgs;
system = host._system;
nixpkgs-channel = host.nixpkgs-channel or "nixpkgs";
})
(lib'.filterAttrs (_: host: (host.format or "iso") == "iso") images');
# We're going to make our custom modules available for our flake. Whether
# or not this is a good thing is debatable, I just want to test it.
nixosModules = lib'.importModules (lib'.filesToAttr ./modules/nixos);
2021-12-06 09:55:40 +00:00
# I can now install home-manager users in non-NixOS systems.
# NICE!
homeConfigurations =
let
users' = listImagesWithSystems users;
in
lib'.mapAttrs
(name: metadata:
let
name = metadata._name;
system = metadata._system;
pkgs = import inputs."${metadata.nixpkgs-channel or "nixpkgs"}" {
inherit system overlays;
};
path = ./users/home-manager/${name};
extraModules = [
({ pkgs, config, ... }: {
2022-11-25 06:35:11 +00:00
# To be able to use the most of our config as possible, we want
# both to use the same overlays.
nixpkgs.overlays = overlays;
2022-09-29 03:35:00 +00:00
# Stallman-senpai will be disappointed. :/
nixpkgs.config.allowUnfree = true;
# Setting the homely options.
home.username = name;
home.homeDirectory = metadata.home-directory or "/home/${config.home.username}";
# home-manager configurations are expected to be deployed on
# non-NixOS systems so it is safe to set this.
programs.home-manager.enable = true;
targets.genericLinux.enable = true;
})
2022-09-29 03:35:00 +00:00
userSharedConfig
nixSettingsSharedConfig
path
];
in
mkHome {
inherit pkgs system extraModules extraArgs;
home-manager-channel = metadata.home-manager-channel or "home-manager";
})
users';
2021-12-06 09:55:40 +00:00
# Extending home-manager with my custom modules, if anyone cares.
homeModules =
lib'.importModules (lib'.filesToAttr ./modules/home-manager);
# In case somebody wants to use my stuff to be included in nixpkgs.
overlays.default = final: prev: import ./pkgs { pkgs = prev; };
2021-12-18 09:41:45 +00:00
# My custom packages, available in here as well. Though, I mainly support
# "x86_64-linux". I just want to try out supporting other systems.
2023-01-07 08:06:34 +00:00
packages = forAllSystems (system:
inputs.flake-utils.lib.flattenTree (import ./pkgs {
pkgs = import nixpkgs { inherit system; };
}));
# This contains images that are meant to be built and distributed
# somewhere else including those NixOS configurations that are built as
# an ISO.
images =
forAllSystems (system:
let
images' = lib'.filterAttrs (host: metadata: lib'.elem system metadata.systems) images;
in
lib'.mapAttrs
(host: metadata:
let
inherit system;
nixpkgs-channel = metadata.nixpkgs-channel or "nixpkgs";
pkgs = import inputs."${nixpkgs-channel}" { inherit system overlays; };
format = metadata.format or "iso";
in
mkImage {
inherit format system pkgs extraArgs;
extraModules = [
({ lib, ... }: {
config = lib.mkMerge [
{ networking.hostName = lib.mkForce metadata.hostname or host; }
(lib.mkIf (metadata ? domain)
{ networking.domain = lib.mkForce metadata.domain; })
];
})
hostSharedConfig
./hosts/${host}
];
})
images');
# My several development shells for usual type of projects. This is much
# more preferable than installing all of the packages at the system
# configuration (or even home environment).
2021-12-18 09:41:45 +00:00
devShells = forAllSystems (system:
let pkgs = import nixpkgs { inherit system overlays; };
in {
default = import ./shell.nix { inherit pkgs; };
2023-07-08 11:41:30 +00:00
docs = import ./docs/shell.nix { inherit pkgs; };
} // (import ./shells { inherit pkgs; }));
# Cookiecutter templates for your mama.
templates = {
default = self.templates.basic-devshell;
basic-devshell = {
path = ./templates/basic-devshell;
description = "Basic development shell template";
};
basic-overlay-flake = {
path = ./templates/basic-overlay-flake;
description = "Basic overlay as a flake";
};
2023-05-24 10:54:07 +00:00
sample-nixos-template = {
path = ./templates/sample-nixos-template;
description = "Simple sample Nix flake with NixOS and home-manager";
};
2023-08-03 07:44:11 +00:00
local-ruby-nix = {
path = ./templates/local-ruby-nix;
description = "Local Ruby app development with ruby-nix";
};
};
# No amount of formatters will make this codebase nicer but it sure does
# feel like it does.
formatter =
2022-11-19 03:00:53 +00:00
forAllSystems (system: nixpkgs.legacyPackages.${system}.treefmt);
# nixops-lite (that is much more powerful than nixops itself)... in
# here!?! We got it all, son!
#
# Also, don't forget to always clean your shell history when overriding
# sensitive info such as the hostname and such. A helpful tip would be
# ignoring the shell entry by simply prefixing it with a space which most
# command-line shells have support for (e.g., Bash, zsh, fish).
2023-01-07 08:06:34 +00:00
deploy.nodes =
let
nixosConfigurations = lib'.mapAttrs'
(name: value:
let
metadata = images.${name};
in
2023-01-07 08:06:34 +00:00
lib'.nameValuePair "nixos-${name}" {
hostname = metadata.deploy.hostname or name;
autoRollback = metadata.deploy.auto-rollback or true;
magicRollback = metadata.deploy.magic-rollback or true;
fastConnection = metadata.deploy.fast-connection or true;
remoteBuild = metadata.deploy.remote-build or false;
2023-01-07 08:06:34 +00:00
profiles.system = {
sshUser = metadata.deploy.ssh-user or "admin";
2023-01-07 08:06:34 +00:00
user = "root";
path = inputs.deploy.lib.${metadata.system or defaultSystem}.activate.nixos value;
2023-01-07 08:06:34 +00:00
};
})
self.nixosConfigurations;
homeConfigurations = lib'.mapAttrs'
2023-01-07 08:06:34 +00:00
(name: value:
let
metadata = users.${name};
username = metadata.deploy.username or name;
in
2023-07-05 08:42:15 +00:00
lib'.nameValuePair "home-manager-${name}" {
hostname = metadata.deploy.hostname or name;
autoRollback = metadata.deploy.auto-rollback or true;
magicRollback = metadata.deploy.magic-rollback or true;
fastConnection = metadata.deploy.fast-connection or true;
remoteBuild = metadata.deploy.remote-build or false;
profiles.home = {
sshUser = metadata.deploy.ssh-user or username;
user = metadata.deploy.user or username;
path = inputs.deploy.lib.${metadata.system or defaultSystem}.activate.home-manager value;
};
})
self.homeConfigurations;
2023-01-07 08:06:34 +00:00
in
nixosConfigurations // homeConfigurations;
# How to make yourself slightly saner than before. So far the main checks
# are for deploy nodes.
checks = lib'.mapAttrs
(system: deployLib: deployLib.deployChecks self.deploy)
inputs.deploy.lib;
2022-11-21 12:33:58 +00:00
# I'm cut off from the rest of my setup with no Hydra instance yet but
# I'm sure it will grow some of them as long as you didn't put it under a
# rock.
hydraJobs.build-packages = forAllSystems (system: self.packages.${system});
2022-01-25 01:32:17 +00:00
};
}