Refactor flake

This commit is contained in:
Henrik Lissner 2020-10-26 05:48:12 -04:00
parent da2eebf5d5
commit 804011f538
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
12 changed files with 164 additions and 138 deletions

68
default.nix Normal file
View file

@ -0,0 +1,68 @@
{ inputs, config, lib, pkgs, ... }:
with lib;
with lib.my;
with inputs;
{
imports =
# I use home-manager to deploy files to $HOME; little else
[ home-manager.nixosModules.home-manager ]
# All my personal modules
++ (mapModulesRec' (toString ./modules) import);
# Common config for all nixos machines; and to ensure the flake operates
# soundly
environment.variables.DOTFILES = dotFilesDir;
# Configure nix and nixpkgs
environment.variables.NIXPKGS_ALLOW_UNFREE = "1";
nix = {
package = pkgs.unstable.nixFlakes;
extraOptions = "experimental-features = nix-command flakes";
nixPath = [
"nixpkgs=${nixos}"
"nixpkgs-unstable=${nixos-unstable}"
"nixpkgs-overlays=${dotFilesDir}/overlays"
"home-manager=${home-manager}"
"dotfiles=${dotFilesDir}"
];
binaryCaches = [
"https://cache.nixos.org/"
"https://nix-community.cachix.org"
];
binaryCachePublicKeys = [
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
];
registry = {
nixos.flake = nixos;
nixpkgs.flake = nixos-unstable;
};
};
system.configurationRevision = mkIf (self ? rev) self.rev;
system.stateVersion = "20.09";
## Some reasonable, global defaults
# This is here to appease 'nix flake check' for generic hosts with no
# hardware-configuration.nix or fileSystem config.
fileSystems."/".device = "/dev/disk/by-label/nixos";
# Use the latest kernel
boot.kernelPackages = pkgs.linuxPackages_latest;
boot.loader = {
efi.canTouchEfiVariables = true;
systemd-boot.configurationLimit = 10;
systemd-boot.enable = mkDefault true;
};
# Just the bear necessities...
environment.systemPackages = with pkgs; [
unstable.cached-nix-shell
coreutils
git
vim
wget
gnumake
];
}

View file

@ -27,12 +27,12 @@
let let
inherit (builtins) baseNameOf; inherit (builtins) baseNameOf;
inherit (lib) nixosSystem mkIf removeSuffix attrNames attrValues; inherit (lib) nixosSystem mkIf removeSuffix attrNames attrValues;
inherit (lib.my) dotFilesDir mapModules mapModulesRec mapModulesRec'; inherit (lib.my) dotFilesDir mapModules mapModulesRec mapHosts;
system = "x86_64-linux"; system = "x86_64-linux";
lib = nixos.lib.extend lib = nixos.lib.extend
(self: super: { my = import ./lib { inherit pkgs; lib = self; }; }); (self: super: { my = import ./lib { inherit pkgs inputs; lib = self; }; });
mkPkgs = pkgs: extraOverlays: import pkgs { mkPkgs = pkgs: extraOverlays: import pkgs {
inherit system; inherit system;
@ -42,6 +42,8 @@
pkgs = mkPkgs nixos [ self.overlay ]; pkgs = mkPkgs nixos [ self.overlay ];
unstable = mkPkgs nixos-unstable []; unstable = mkPkgs nixos-unstable [];
in { in {
lib = lib.my;
overlay = overlay =
final: prev: { final: prev: {
inherit unstable; inherit unstable;
@ -49,63 +51,17 @@
}; };
overlays = overlays =
mapModules (toString ./overlays) import; mapModules ./overlays import;
packages."${system}" = packages."${system}" =
mapModules (toString ./packages) mapModules ./packages
(p: pkgs.callPackage p {}); (p: pkgs.callPackage p {});
nixosModules = nixosModules =
mapModulesRec (toString ./modules) import; { dotfiles = import ./.; }
// mapModulesRec ./modules import;
nixosConfigurations = nixosConfigurations =
mapModules (toString ./hosts) mapHosts ./hosts { inherit system; };
(modulePath: nixosSystem {
inherit system;
specialArgs = { inherit lib inputs; };
modules = [
# Common config for all nixos machines; and to ensure the flake
# operates soundly
{
networking.hostName = removeSuffix ".nix" (baseNameOf modulePath);
environment.variables.NIXPKGS_ALLOW_UNFREE = "1";
environment.variables.DOTFILES = dotFilesDir;
nixpkgs.pkgs = pkgs;
nix = {
package = unstable.nixFlakes;
extraOptions = "experimental-features = nix-command flakes";
nixPath = [
"nixpkgs=${nixos}"
"nixpkgs-unstable=${nixos-unstable}"
"nixpkgs-overlays=${dotFilesDir}/overlays"
"home-manager=${home-manager}"
"dotfiles=${dotFilesDir}"
];
binaryCaches = [
"https://cache.nixos.org/"
"https://nix-community.cachix.org"
];
binaryCachePublicKeys = [
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
];
registry = {
nixos.flake = nixos;
nixpkgs.flake = nixos-unstable;
};
};
system.configurationRevision = mkIf (self ? rev) self.rev;
system.stateVersion = "20.09";
}
# I use home-manager to deploy files to $HOME; little else
home-manager.nixosModules.home-manager
# All my personal modules
{ imports = mapModulesRec' (toString ./modules) import; }
./hosts # config common to all hosts
modulePath # host-specific config
];
});
}; };
} }

View file

@ -4,7 +4,10 @@
with lib.my; with lib.my;
{ {
imports = [ ./hardware-configuration.nix ]; imports = [
../personal.nix
./hardware-configuration.nix
];
## Modules ## Modules
modules = { modules = {
@ -20,7 +23,6 @@ with lib.my;
## Local config ## Local config
boot.loader.systemd-boot.enable = true;
networking.networkmanager.enable = true; networking.networkmanager.enable = true;
# nginx hosts # nginx hosts

View file

@ -1,71 +0,0 @@
# hosts/default.nix
#
# Loads config common to all hosts.
{ config, lib, pkgs, ... }:
with lib;
with lib.my;
{
## Some reasonable, global defaults
# This is here to appease 'nix flake check' for generic hosts with no
# hardware-configuration.nix or fileSystem config.
fileSystems."/".device = "/dev/disk/by-label/nixos";
boot.loader = {
efi.canTouchEfiVariables = true;
systemd-boot.configurationLimit = 10;
};
# Just the bear necessities...
environment.systemPackages = with pkgs; [
unstable.cached-nix-shell
coreutils
git
vim
wget
gnumake
];
networking.firewall.enable = true;
networking.hosts =
let hostConfig = {
"192.168.1.2" = [ "ao" ];
"192.168.1.3" = [ "aka" ];
"192.168.1.10" = [ "kuro" ];
"192.168.1.11" = [ "shiro" ];
"192.168.1.12" = [ "midori" ];
};
hosts = flatten (attrValues hostConfig);
hostName = config.networking.hostName;
in mkIf (builtins.elem hostName hosts) hostConfig;
# Use the latest kernel
boot.kernelPackages = pkgs.linuxPackages_latest;
## Location config -- since Toronto is my 127.0.0.1
time.timeZone = lib.mkDefault "America/Toronto";
i18n.defaultLocale = lib.mkDefault "en_US.UTF-8";
# For redshift, mainly
location = (if config.time.timeZone == "America/Toronto" then {
latitude = 43.70011;
longitude = -79.4163;
} else if config.time.timeZone == "Europe/Copenhagen" then {
latitude = 55.88;
longitude = 12.5;
} else {});
## System security tweaks
boot.tmpOnTmpfs = true;
security.hideProcessInformation = true;
security.protectKernelImage = true;
# Fix a security hole in place for backwards compatibility. See desc in
# nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix
boot.loader.systemd-boot.editor = false;
# Change me later!
user.initialPassword = "nix";
users.users.root.initialPassword = "nix";
}

View file

@ -1,6 +1,9 @@
{ ... }: { ... }:
{ {
imports = [ ./hardware-configuration.nix ]; imports = [
../personal.nix
./hardware-configuration.nix
];
## Modules ## Modules
modules = { modules = {
@ -73,7 +76,6 @@
programs.ssh.startAgent = true; programs.ssh.startAgent = true;
services.openssh.startWhenNeeded = true; services.openssh.startWhenNeeded = true;
boot.loader.systemd-boot.enable = true;
networking.networkmanager.enable = true; networking.networkmanager.enable = true;
# The global useDHCP flag is deprecated, therefore explicitly set to false # The global useDHCP flag is deprecated, therefore explicitly set to false
# here. Per-interface useDHCP will be mandatory in the future, so this # here. Per-interface useDHCP will be mandatory in the future, so this

View file

@ -27,9 +27,7 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
let with lib;
inherit (lib) filter pathExists;
in
{ {
imports = filter pathExists ["/etc/nixos/configuration.nix"]; imports = filter pathExists ["/etc/nixos/configuration.nix"];

28
hosts/personal.nix Normal file
View file

@ -0,0 +1,28 @@
{ config, lib, ... }:
with lib;
{
networking.hosts =
let hostConfig = {
"192.168.1.2" = [ "ao" ];
"192.168.1.3" = [ "aka" ];
"192.168.1.10" = [ "kuro" ];
"192.168.1.11" = [ "shiro" ];
"192.168.1.12" = [ "midori" ];
};
hosts = flatten (attrValues hostConfig);
hostName = config.networking.hostName;
in mkIf (builtins.elem hostName hosts) hostConfig;
## Location config -- since Toronto is my 127.0.0.1
time.timeZone = mkDefault "America/Toronto";
i18n.defaultLocale = mkDefault "en_US.UTF-8";
# For redshift, mainly
location = (if config.time.timeZone == "America/Toronto" then {
latitude = 43.70011;
longitude = -79.4163;
} else if config.time.timeZone == "Europe/Copenhagen" then {
latitude = 55.88;
longitude = 12.5;
} else {});
}

View file

@ -2,7 +2,10 @@
{ ... }: { ... }:
{ {
imports = [ ./hardware-configuration.nix ]; imports = [
../personal.nix
./hardware-configuration.nix
];
## Modules ## Modules
modules = { modules = {
@ -77,7 +80,6 @@
programs.ssh.startAgent = true; programs.ssh.startAgent = true;
services.openssh.startWhenNeeded = true; services.openssh.startWhenNeeded = true;
boot.loader.systemd-boot.enable = true;
networking.wireless.enable = true; networking.wireless.enable = true;
hardware.opengl.enable = true; hardware.opengl.enable = true;

View file

@ -1,4 +1,4 @@
{ lib, pkgs, ... }: { inputs, lib, pkgs, ... }:
let let
inherit (lib) makeExtensible attrValues foldr; inherit (lib) makeExtensible attrValues foldr;
@ -10,8 +10,8 @@ let
}; };
mylib = makeExtensible (self: mylib = makeExtensible (self:
with self; mapModules (toString ./.) with self; mapModules ./.
(file: import file { inherit self lib pkgs; })); (file: import file { inherit self lib pkgs inputs; }));
in in
mylib.extend mylib.extend
(self: super: (self: super:

25
lib/nixos.nix Normal file
View file

@ -0,0 +1,25 @@
{ inputs, lib, pkgs, ... }:
with lib;
with lib.my;
let system = "x86_64-linux";
in {
mkHost = path: attrs @ { system ? system, ... }:
nixosSystem {
inherit system;
specialArgs = { inherit lib inputs; };
modules = [
{
nixpkgs.pkgs = pkgs;
networking.hostName = mkDefault (removeSuffix ".nix" (baseNameOf path));
}
(filterAttrs (n: v: !elem n [ "system" ]) attrs)
../.
(import path)
];
};
mapHosts = dir: attrs @ { system ? system, ... }:
mapModules dir
(hostPath: mkHost hostPath attrs);
}

View file

@ -2,7 +2,7 @@
# https://github.com/hlissner/doom-emacs. This module sets it up to meet my # https://github.com/hlissner/doom-emacs. This module sets it up to meet my
# particular Doomy needs. # particular Doomy needs.
{ config, options, lib, pkgs, inputs, ... }: { config, lib, pkgs, inputs, ... }:
with lib; with lib;
with lib.my; with lib.my;

16
modules/security.nix Normal file
View file

@ -0,0 +1,16 @@
{ ... }:
{
## System security tweaks
boot.tmpOnTmpfs = true;
security.hideProcessInformation = true;
security.protectKernelImage = true;
# Fix a security hole in place for backwards compatibility. See desc in
# nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot.nix
boot.loader.systemd-boot.editor = false;
# Change me later!
user.initialPassword = "nix";
users.users.root.initialPassword = "nix";
}