Skip to content

Commit

Permalink
flake: init
Browse files Browse the repository at this point in the history
  • Loading branch information
freezeboy committed Nov 29, 2020
0 parents commit 6f7b61a
Show file tree
Hide file tree
Showing 25 changed files with 722 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
watch_file flake.nix
watch_file flake.lock
# load the flake devShell
eval "$([ -d $(direnv_layout_dir) ] || mkdir $(direnv_layout_dir); nix print-dev-env --profile "$(direnv_layout_dir)/flake-profile")"
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore nix links
/result
/result-*

.direnv

# For the moment, ignore /secrets content
# will change it when implementing it
/secrets/*
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Flake structure

## Objective

This is an example of how I structure my configurations,
not necessarily the perfect match for every setup.

## Features

This flake example shows how to group several common tools:

* Ability to have custom packages in the system
* Ability to present this packages as an overlay
* Ability to handle one or multiple `nixos` configurations
* Ability to handle one or multiple `nix-darwin` configurations
* Ability to handle one or multiple `home-manager` configurations

## Usage

1) Enter the devShell environment (using `direnv` or `nix develop`).
2) Update the `inputs` using `flake-mgr update`
3) Generate the system configuration using `flake-mgr switch`
* This will define in the flake registy two local variations
`nixpkgs` to pin it for the system and `nixcfg` to this current
flake.
* Will apply `nixos-rebuild switch` using current flake (a flag
might be required to select the correct configuration).
4) Generate the home configuration using `flake-mgr home-switch`

## Limitations

Currently, all the features were not used / tested (most notable
`nix-darwin`). Flake-mgr doesn't include yet a darwin-switch action.

Secrets handling is still a WIP, I target the integration of
`sops-nix` but other solutions might also apply.

Even though the current flake has an overlay, it is not propagated to
the `nixpkgs` entry in the registry, you will have to combine
explicitely the different overlays from other flakes/shells.

## Directory layout

* `darwin/configurations`: Configurations for `nix-darwin`
* `darwin/modules`: Modules for `nix-darwin`
* `home/configurations`: Configurations for the users
using `home-manager`
* `home/modules`: Modules for `home-manager`
* `lib`: Nix libraries to help handling the flake
* `nixos/configurations`: Configurations for `nixos`
* `nixos/modules`: Modules for `nixos`
* `pkgs`: Local packages
* `secrets`: Not yey implemented, but should contain
keys, credentials and tokens protected by `sops-nix`
or `git-crypt`
1 change: 1 addition & 0 deletions darwin/modules/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
66 changes: 66 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
description = "My custom flake";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-20.09-small";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
home-manager.url = "github:nix-community/home-manager/release-20.09";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, home-manager, nixpkgs-unstable }:
let
lib = import ./lib { inherit nixpkgs self; };
in
{
inherit lib;
legacyPackages = lib.utils.forAllSystems (system:
let
rawPkgs = import ./pkgs {
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
};
unstable = import nixpkgs-unstable {
inherit system;
config.allowUnfree = true;
};
inherit home-manager;
};
in
# Not working atm
#lib.utils.removeIncompatible system rawPkgs;
rawPkgs
);

homeConfigurations = lib.utils.forAllSystems (system: (lib.homeManager.loadConfigurations { inherit system; }).homeConfigurations);
homeModules = lib.utils.forAllSystems (system: (lib.homeManager.loadConfigurations { inherit system; }).homeModules);

inherit (lib.utils) overlay;
inherit (lib.nixos.loadConfigurations home-manager.nixosModules)
nixosModules nixosConfigurations;
inherit (lib.darwin.loadConfigurations)
darwinModules darwinConfigurations;

devShell = lib.utils.forAllSystems (system:
nixpkgs.legacyPackages.${system}.mkShell {
buildInputs =
(with self.legacyPackages.${system}; [
flake-mgr
]);
});
};
}
7 changes: 7 additions & 0 deletions home/configurations/freezeboy/home.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ pkgs, ... }:

{
home.packages = with pkgs; [
ripgrep
];
}
1 change: 1 addition & 0 deletions home/modules/common/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
24 changes: 24 additions & 0 deletions lib/darwin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ self, nixpkgs, utils }:

let
darwinModules = import ../darwin/modules;

in
{
inherit darwinModules;

loadConfigurations = {
system ? builtins.currentSystem
, pkgs ? nixpkgs.legacyPackages.${system}
}:
{
inherit darwinModules;

darwinConfigurations = utils.loadConfigurations "${self}/darwin"
({name, fullName }: {
configuration = "${fullName}/configuration.nix";
username = name;
inherit system pkgs;
});
};
}
32 changes: 32 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{ self, nixpkgs }:

let
utils = import ./utils.nix { inherit self nixpkgs; };

combinedPackages = nixpkgs // {
legacyPackages = utils.forAllSystems (system:
import nixpkgs {
inherit system;
config.allowUnfree = true;
overlays = [ self.overlay ];
});
};
in
{
inherit utils;

nixos = import ./nixos.nix {
inherit self utils;
nixpkgs = combinedPackages;
};

homeManager = import ./home-manager.nix {
inherit self utils;
nixpkgs = combinedPackages;
};

darwin = import ./darwin.nix {
inherit self utils;
nixpkgs = combinedPackages;
};
}
40 changes: 40 additions & 0 deletions lib/home-manager.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{ self, nixpkgs, utils }:

let
homeModules = import ../home/modules;

configuration = {
configuration
, username
, homeDirectory ? "/home/${username}"
, pkgs ? nixpkgs.legacyPackages.${system}
, system ? builtins.currentSystem
, check ? true }:
import "${self.legacyPackages.${system}.unstable.home-manager}/modules" {
inherit check pkgs;
configuration = { ... }: {
imports = [ configuration homeModules ];
_module.args.pkgs = pkgs;
home = { inherit homeDirectory username; };
};
};
in

{
inherit homeModules;

loadConfigurations = {
system ? builtins.currentSystem
, pkgs ? nixpkgs.legacyPackages.${system}
}:
{
inherit homeModules;

homeConfigurations = utils.loadConfigurations "${self}/home"
({name, fullName }: configuration {
configuration = "${fullName}/home.nix";
username = name;
inherit system pkgs;
});
};
}
41 changes: 41 additions & 0 deletions lib/nixos.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{ self, nixpkgs, utils }:

let
inherit (nixpkgs) lib;
inherit (utils) filterDir mapDir;

fromCommonConfiguration = nixosModules: extraModule: rec {
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
modules = (lib.mapAttrsToList (n: v: v) nixosModules)
++ [ {
imports = [
nixpkgs.nixosModules.notDetected
extraModule
];

system.configurationRevision = lib.mkIf (self ? rev) self.rev;

nix.registry = {
nixpkgs.flake = nixpkgs;
nixcfg.flake = self;
};
} ];
};

nixosModules =
let mods = "${self}/nixos/modules"; in mapDir mods
(name: value: if value == "regular" then import "${mods}/${name}.nix" else import "${mods}/${name}")
(n: v: n != "default.nix");
in {
inherit nixosModules;

loadConfigurations = extraModules:
{
inherit nixosModules;
nixosConfigurations = utils.loadConfigurations "${self}/nixos"
({fullName, ... }: lib.nixosSystem
(fromCommonConfiguration (nixosModules // extraModules)
(import "${fullName}/configuration.nix")));
};
}
Loading

0 comments on commit 6f7b61a

Please sign in to comment.