From 1b8080651684a889c028653e7f8b55cd6db80aa0 Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Tue, 19 Jan 2021 00:51:23 -0700 Subject: [PATCH] compat: fixes * fix nixos-option * use flake-compat for all backward compat concerns * rebuild devshell after any nix file changes * reexport system pkgs as legacyPackages * provide lib flake output * provide flake lib to nixos configs via `lib.flk` * clean up flake.nix --- .envrc | 2 +- compat/default.nix | 13 +++++ compat/fetch.nix | 17 ------- compat/nixos/default.nix | 10 ++++ compat/nixpkgs.nix | 5 -- configuration.nix | 41 --------------- default.nix | 17 ++----- flake.nix | 74 ++++++++++++++------------- hosts/default.nix | 9 ++-- lib/{utils.nix => default.nix} | 14 +++-- profiles/core/default.nix | 3 ++ shell.nix | 93 +--------------------------------- shell/default.nix | 89 ++++++++++++++++++++++++++++++++ 13 files changed, 173 insertions(+), 214 deletions(-) create mode 100644 compat/default.nix delete mode 100644 compat/fetch.nix create mode 100644 compat/nixos/default.nix delete mode 100644 compat/nixpkgs.nix delete mode 100644 configuration.nix rename lib/{utils.nix => default.nix} (95%) create mode 100644 shell/default.nix diff --git a/.envrc b/.envrc index 77ce6204..8d15fd65 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,2 @@ -watch_file shell.nix +watch_file **/*.nix use flake || use nix diff --git a/compat/default.nix b/compat/default.nix new file mode 100644 index 00000000..91d7ff93 --- /dev/null +++ b/compat/default.nix @@ -0,0 +1,13 @@ +let + flake = (import + ( + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz"; + sha256 = "sha256:0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; + } + ) + { + src = ../.; + }); +in +flake diff --git a/compat/fetch.nix b/compat/fetch.nix deleted file mode 100644 index 01f0f02f..00000000 --- a/compat/fetch.nix +++ /dev/null @@ -1,17 +0,0 @@ -let - inherit (builtins) - fetchTarball - fromJSON - readFile - ; - lockfile = fromJSON (readFile ../flake.lock); -in -input: -let - locked = lockfile.nodes."${input}".locked; - inherit (locked) rev narHash owner repo; -in -fetchTarball { - url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; - sha256 = narHash; -} diff --git a/compat/nixos/default.nix b/compat/nixos/default.nix new file mode 100644 index 00000000..e4e0e68a --- /dev/null +++ b/compat/nixos/default.nix @@ -0,0 +1,10 @@ +{ ... }: +let + inherit (default.inputs.nixos) lib; + + host = configs.${hostname} or configs.NixOS; + configs = default.nixosConfigurations; + default = (import ../.).defaultNix; + hostname = lib.fileContents /etc/hostname; +in +host diff --git a/compat/nixpkgs.nix b/compat/nixpkgs.nix deleted file mode 100644 index 3dd503a9..00000000 --- a/compat/nixpkgs.nix +++ /dev/null @@ -1,5 +0,0 @@ -let - fetch = import ./fetch.nix; - nixpkgs = fetch "nixos"; -in -nixpkgs diff --git a/configuration.nix b/configuration.nix deleted file mode 100644 index 6ffcacfe..00000000 --- a/configuration.nix +++ /dev/null @@ -1,41 +0,0 @@ -# this file is an impure recreation of the flake profile currently deployed -# based on the systems hostname. The purpose is so tools which do not yet have -# flake support (e.g `nixos-option`), can work as expected. -{ lib, ... }: -let - inherit (builtins) attrNames readDir; - - nixpkgs = toString (import ./nixpkgs-compat.nix); - - hostname = lib.fileContents /etc/hostname; - host = "/etc/nixos/hosts/${hostname}.nix"; - config = - if (builtins.pathExists host) then - [ host ] - else - [ /etc/nixos/hosts/NixOS.nix ]; -in -{ - imports = (import ./modules/list.nix) ++ [ - "${ - builtins.fetchTarball - "https://github.com/nix-community/home-manager/archive/master.tar.gz" - }/nixos" - /etc/nixos/profiles/core - ] ++ config; - - networking.hostName = hostname; - nix.nixPath = [ - "nixpkgs=${nixpkgs}" - "nixos-config=/etc/nixos/configuration.nix" - "nixpkgs-overlays=/etc/nixos/overlays" - ]; - - nixpkgs.overlays = - let - overlays = map - (name: import (./overlays + "/${name}")) - (attrNames (readDir ./overlays)); - in - overlays; -} diff --git a/default.nix b/default.nix index 85206d42..bd9177a7 100644 --- a/default.nix +++ b/default.nix @@ -1,15 +1,6 @@ let - inherit (flake.inputs.nixos.lib) recurseIntoAttrs mapAttrs; - flake = (import - ( - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz"; - sha256 = "sha256:0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; - } - ) - { - src = ./.; - }).defaultNix; - recurse = mapAttrs (_: v: recurseIntoAttrs v) flake.packages; + inherit (default.inputs.nixos.lib) recurseIntoAttrs; + + default = (import ./compat).defaultNix; in -recurse +builtins.mapAttrs (_: v: recurseIntoAttrs v) default.packages diff --git a/flake.nix b/flake.nix index 73d04eab..d2827732 100644 --- a/flake.nix +++ b/flake.nix @@ -15,48 +15,34 @@ outputs = inputs@{ self, home, nixos, master, flake-utils, nur, devshell, nixos-hardware }: let - inherit (builtins) attrNames attrValues elem pathExists; - inherit (flake-utils.lib) eachDefaultSystem mkApp flattenTreeSystem; - inherit (nixos) lib; - inherit (lib) recursiveUpdate filterAttrs mapAttrs; - inherit (utils) pathsToImportedAttrs genPkgset overlayPaths modules - genPackages pkgImport; - - utils = import ./lib/utils.nix { inherit lib; }; + inherit (builtins) attrValues; + inherit (flake-utils.lib) eachDefaultSystem flattenTreeSystem; + inherit (nixos.lib) recursiveUpdate; + inherit (self.lib) overlays nixosModules genPackages pkgImport; externOverlays = [ nur.overlay devshell.overlay ]; externModules = [ home.nixosModules.home-manager ]; - pkgs' = unstable: - let - override = import ./pkgs/override.nix; - overlays = (attrValues self.overlays) - ++ externOverlays - ++ [ self.overlay (override unstable) ]; - in - pkgImport nixos overlays; - - unstable' = pkgImport master [ ]; - - osSystem = "x86_64-linux"; - outputs = let - system = osSystem; - unstablePkgs = unstable' system; - osPkgs = pkgs' unstablePkgs system; + system = "x86_64-linux"; + pkgs = self.legacyPackages.${system}; in { + inherit nixosModules overlays; + nixosConfigurations = - import ./hosts (recursiveUpdate inputs { - inherit lib osPkgs unstablePkgs utils externModules system; - }); + import ./hosts + (recursiveUpdate inputs { + inherit pkgs externModules system; + inherit (pkgs) lib; + }); overlay = import ./pkgs; - overlays = pathsToImportedAttrs overlayPaths; - - nixosModules = modules; + lib = import ./lib { + inherit (nixos) lib; + }; templates.flk.path = ./.; @@ -69,8 +55,26 @@ (eachDefaultSystem (system: let - unstablePkgs = unstable' system; - pkgs = pkgs' unstablePkgs system; + unstable = pkgImport master [ ] system; + + pkgs = + let + override = import ./pkgs/override.nix; + overlays = (attrValues self.overlays) + ++ externOverlays + ++ [ + self.overlay + (override unstable) + (final: prev: { + lib = (prev.lib or { }) // { + inherit (nixos.lib) nixosSystem; + flk = self.lib; + utils = flake-utils.lib; + }; + }) + ]; + in + pkgImport nixos overlays system; packages = flattenTreeSystem system (genPackages { @@ -80,9 +84,11 @@ { inherit packages; - devShell = import ./shell.nix { - inherit pkgs; + devShell = import ./shell { + inherit pkgs nixos; }; + + legacyPackages = pkgs; } ) ) diff --git a/hosts/default.nix b/hosts/default.nix index d33c1118..db822f4d 100644 --- a/hosts/default.nix +++ b/hosts/default.nix @@ -3,16 +3,14 @@ , nixos , master , nixos-hardware -, osPkgs +, pkgs , self , system -, unstablePkgs -, utils , externModules , ... }: let - inherit (utils) recImport; + inherit (lib.flk) recImport; inherit (builtins) attrValues removeAttrs; unstableModules = [ ]; @@ -50,12 +48,11 @@ let [ "nixos-unstable=${master}" "nixpkgs=${nixos}" - "nixos-config=${path}/configuration.nix" "nixpkgs-overlays=${path}/overlays" "home-manager=${home}" ]; - nixpkgs.pkgs = osPkgs; + nixpkgs = { inherit pkgs; }; nix.registry = { master.flake = master; diff --git a/lib/utils.nix b/lib/default.nix similarity index 95% rename from lib/utils.nix rename to lib/default.nix index 8dc6d9e6..77f89d62 100644 --- a/lib/utils.nix +++ b/lib/default.nix @@ -14,6 +14,7 @@ let # Generate an attribute set by mapping a function over a list of values. genAttrs' = values: f: listToAttrs (map f values); + # pkgImport :: Nixpkgs -> Overlays -> System -> Pkgs pkgImport = nixpkgs: overlays: system: import nixpkgs { inherit system overlays; @@ -23,16 +24,13 @@ let # Convert a list to file paths to attribute set # that has the filenames stripped of nix extension as keys # and imported content of the file as value. + # pathsToImportedAttrs = paths: genAttrs' paths (path: { name = removeSuffix ".nix" (baseNameOf path); value = import path; }); -in -{ - inherit mapFilterAttrs genAttrs' pkgImport pathsToImportedAttrs; - overlayPaths = let overlayDir = ../overlays; @@ -40,6 +38,12 @@ in in map fullPath (attrNames (readDir overlayDir)); +in +{ + inherit mapFilterAttrs genAttrs' pkgImport pathsToImportedAttrs; + + overlays = pathsToImportedAttrs overlayPaths; + recImport = { dir, _import ? base: import "${dir}/${base}.nix" }: mapFilterAttrs (_: v: v != null) @@ -51,7 +55,7 @@ in nameValuePair ("") (null)) (readDir dir); - modules = + nixosModules = let # binary cache cachix = import ../cachix.nix; diff --git a/profiles/core/default.nix b/profiles/core/default.nix index 68652142..b000f37e 100644 --- a/profiles/core/default.nix +++ b/profiles/core/default.nix @@ -74,6 +74,9 @@ in manix "" | grep '^# ' | sed 's/^# \(.*\) (.*/\1/;s/ (.*//;s/^# //' | sk --preview="manix '{}'" | xargs manix ''; + # fix nixos-option + nixos-option = "nixos-option -I nixpkgs=${toString ../../compat}"; + # sudo s = ifSudo "sudo -E "; si = ifSudo "sudo -i"; diff --git a/shell.nix b/shell.nix index 703a628d..c05730d0 100644 --- a/shell.nix +++ b/shell.nix @@ -1,92 +1 @@ -let - nixpkgs = import ./compat/nixpkgs.nix; - fetch = import ./compat/fetch.nix; - - devshell' = fetch "devshell"; - pkgs' = import devshell' { inherit nixpkgs; }; -in -{ pkgs ? pkgs', ... }: -let - build = "config.system.build"; - - installPkgs = (import "${nixpkgs}/nixos" { - configuration = { }; - system = pkgs.system; - }).config.system.build; - - flk = pkgs.writeShellScriptBin "flk" '' - if [[ -z "$1" ]]; then - echo "Usage: $(basename "$0") [ iso | up | install {host} | {host} [switch|boot|test] ]" - elif [[ "$1" == "up" ]]; then - mkdir -p up - hostname=$(hostname) - nixos-generate-config --dir up/$hostname - echo \ - "{ - imports = [ ../up/$hostname/configuration.nix ]; - }" > hosts/up-$hostname.nix - git add -f up/$hostname - git add -f hosts/up-$hostname.nix - elif [[ "$1" == "iso" ]]; then - nix build $DEVSHELL_ROOT#nixosConfigurations.niximg.${build}.isoImage "${"\${@:2}"}" - elif [[ "$1" == "install" ]]; then - sudo nixos-install --flake ".#$2" "${"\${@:3}"}" - else - sudo nixos-rebuild --flake ".#$1" "${"\${@:2}"}" - fi - ''; - - name = "flk"; -in -pkgs.mkDevShell { - inherit name; - - packages = with pkgs; with installPkgs; [ - git-crypt - nixos-install - nixos-generate-config - nixos-enter - pre-commit - ]; - - env = { inherit name; }; - - commands = with pkgs; [ - { - name = nixpkgs-fmt.pname; - package = nixpkgs-fmt; - help = nixpkgs-fmt.meta.description; - category = "linters"; - } - { - name = flk.name; - help = "Build, deploy, and install nixflk"; - category = "main"; - package = flk; - } - { - name = "hooks"; - help = "install git hooks"; - command = "pre-commit install"; - } - { - name = "grip"; - help = python38Packages.grip.meta.description; - category = "servers"; - package = python38Packages.grip; - } - { - name = git.pname; - help = git.meta.description; - category = "vcs"; - package = git; - } - { - name = "nix"; - help = nixFlakes.meta.description; - category = "main"; - command = ''${nixFlakes}/bin/nix --option experimental-features "nix-command flakes ca-references" "$@"''; - } - ]; - -} +(import ./compat).shellNix diff --git a/shell/default.nix b/shell/default.nix new file mode 100644 index 00000000..682e3617 --- /dev/null +++ b/shell/default.nix @@ -0,0 +1,89 @@ +{ pkgs ? (import ./compat).defaultNix.legacyPackages."${builtins.currentSystem}" +, nixos ? (import ./compat).defaultNix.inputs.nixos +, ... +}: +let + build = "config.system.build"; + + installPkgs = (import "${nixos}/nixos" { + configuration = { }; + system = pkgs.system; + }).config.system.build; + + flk = pkgs.writeShellScriptBin "flk" '' + if [[ -z "$1" ]]; then + echo "Usage: $(basename "$0") [ iso | up | install {host} | {host} [switch|boot|test] ]" + elif [[ "$1" == "up" ]]; then + mkdir -p up + hostname=$(hostname) + nixos-generate-config --dir up/$hostname + echo \ + "{ + imports = [ ../up/$hostname/configuration.nix ]; + }" > hosts/up-$hostname.nix + git add -f up/$hostname + git add -f hosts/up-$hostname.nix + elif [[ "$1" == "iso" ]]; then + nix build $DEVSHELL_ROOT#nixosConfigurations.niximg.${build}.isoImage "${"\${@:2}"}" + elif [[ "$1" == "install" ]]; then + sudo nixos-install --flake ".#$2" "${"\${@:3}"}" + else + sudo nixos-rebuild --flake ".#$1" "${"\${@:2}"}" + fi + ''; + + name = "flk"; +in +pkgs.mkDevShell { + inherit name; + + packages = with pkgs; with installPkgs; [ + git-crypt + nixos-install + nixos-generate-config + nixos-enter + nixos-option + pre-commit + ]; + + env = { inherit name; }; + + commands = with pkgs; [ + { + name = nixpkgs-fmt.pname; + package = nixpkgs-fmt; + help = nixpkgs-fmt.meta.description; + category = "linters"; + } + { + name = flk.name; + help = "Build, deploy, and install nixflk"; + category = "main"; + package = flk; + } + { + name = "hooks"; + help = "install git hooks"; + command = "pre-commit install"; + } + { + name = "grip"; + help = python38Packages.grip.meta.description; + category = "servers"; + package = python38Packages.grip; + } + { + name = git.pname; + help = git.meta.description; + category = "vcs"; + package = git; + } + { + name = "nix"; + help = nixFlakes.meta.description; + category = "main"; + command = ''${nixFlakes}/bin/nix --option experimental-features "nix-command flakes ca-references" "$@"''; + } + ]; + +}