summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-04-29 14:48:08 +0200
committerAlan Pearce2024-05-18 12:17:12 +0200
commitec92b62d48052d2e4074b194faee86bf7ad18144 (patch)
tree5c14725810d3ac531d13da9ae18f0df8ba43a485
parent160dacb051bb3529f33e8d388818f63db810b3a1 (diff)
downloadnixfiles-ec92b62d48052d2e4074b194faee86bf7ad18144.tar.lz
nixfiles-ec92b62d48052d2e4074b194faee86bf7ad18144.tar.zst
nixfiles-ec92b62d48052d2e4074b194faee86bf7ad18144.zip
set up lorri
l---------darwin-rebuild2
-rwxr-xr-xhome-manager133
-rw-r--r--nix/sources.json176
l---------nixos-rebuild2
-rw-r--r--shell.nix29
-rw-r--r--system/settings/darwin.nix5
6 files changed, 253 insertions, 94 deletions
diff --git a/darwin-rebuild b/darwin-rebuild
index 856a8b68..2eaccdef 120000
--- a/darwin-rebuild
+++ b/darwin-rebuild
@@ -1 +1 @@
-/Users/alan/projects/alanpearce.eu/nixfiles/home-manager
\ No newline at end of file
+home-manager
\ No newline at end of file
diff --git a/home-manager b/home-manager
index 08cfd216..f351b2f3 100755
--- a/home-manager
+++ b/home-manager
@@ -1 +1,132 @@
-nix-shell --run "$(basename $0) $@"
+#!/usr/bin/env fish
+
+if not set --query HOME
+    echo "error: no HOME"
+    exit 1
+end
+
+set --function channel_root $HOME/.nix-defexpr/channels
+set --function user_nixpkgs $HOME/.nixpkgs
+
+if set --query XDG_CONFIG_HOME
+    set --function hm_config_dir $XDG_CONFIG_HOME/home-manager
+else
+    set --function hm_config_dir $HOME/.config/home-manager
+end
+
+for i in 1
+    if test ! -d $user_nixpkgs
+        echo "user nixpkgs folder $user_nixpkgs does not exist, creating it"
+        mkdir -p $user_nixpkgs
+    end
+    if test -L $hm_config_dir -a (path resolve $hm_config_dir) = (path resolve (status basename))
+        if test y = (read --nchars=1 --prompt-str="$hm_config_dir already exists as a symlink to the current directory. Remove it [yN]? ")
+            unlink $hm_config_dir
+        end
+    end
+    if test ! -d $hm_config_dir
+        echo "user home-manager configuration folder $hm_config_dir does not exist, creating it"
+        mkdir -p $hm_config_dir
+    end
+    if test ! -e $channel_root
+        if test -L $channel_root
+            # broken symlink
+            rm $channel_root
+        end
+        echo "channel root $channel_root does not exist, creating it"
+        mkdir -p $channel_root
+    else
+        if test ! -d $channel_root
+            echo "error: $channel_root is not a directory"
+        else if test -L $channel_root
+            echo "error: $channel_root is a symlink, please remove it"
+        else if path is --invert --perm write $channel_root
+            echo "error: $channel_root is not writable; might be a symlink"
+        else if path is --invert --type link $channel_root/*
+            for p in (path filter --invert --type link $channel_root/*)
+                # might be a broken symlink
+                if test -L $p -a ! -e $p
+                    rm $p
+                else
+                    echo "error: channel $p is not a broken symlink, don't know what to do with it"
+                end
+            end
+            # check again and fail if there are still unknown files
+            if path is --invert --type link $channel_root/*
+                echo "error: non-link(s) in $channel_root"
+                path filter --invert --type link $channel_root/*
+            else
+                continue
+            end
+        else
+            continue # with script
+        end
+        exit 1
+    end
+end
+
+set --function current_script_name (status basename)
+switch $current_script_name
+    case darwin-rebuild
+        set --local darwin_config_source (path resolve (status dirname)/system/$hostname.nix)
+        set --local darwin_config_target $user_nixpkgs/darwin-configuration.nix
+        if test ! -e $darwin_config_target
+            ln -s $darwin_config_source $darwin_config_target
+        end
+        set --append argv -I darwin-config=$darwin_config_source
+    case nixos-rebuild
+        set --local nixos_config_source (path resolve (status dirname)/system/$hostname.nix)
+        set --local nixos_config_target $user_nixpkgs/configuration.nix
+        if test ! -e $nixos_config_target
+            ln -s $nixos_config_source $nixos_config_target
+        end
+        if ! fish_is_root_user
+            set --append argv --use-remote-sudo
+        end
+        set --append argv -I nixos-config=$nixos_config_source
+    case home-manager
+        set --local hm_config_source (path resolve (status dirname)/user/$hostname.nix)
+        set --local hm_config_target $hm_config_dir/home.nix
+        if test ! -e $hm_config_target
+            ln -s $hm_config_source $hm_config_target
+        end
+        set --export HOME_MANAGER_CONFIG $hm_config_source
+end
+
+set --function old_channels (path basename $channel_root/*)
+set --function --export HOME_MANAGER_CONFIG (path resolve (status dirname)/user/$hostname.nix)
+
+for np in $NIX_PATH
+    echo $np | read --function --delimiter "=" channel new_src_rel
+    set --local new_src (path resolve $new_src_rel)
+
+    if test $channel = darwin-config
+        continue
+    end
+    if set --local i (contains --index $channel $old_channels)
+        set --erase old_channels[$i]
+    end
+    set --local target $channel_root/$channel
+    set --local src (path resolve $target)
+    if test -e $src
+        if test $src = $new_src
+            # no need to re-link it
+            continue
+        else
+            unlink $target
+        end
+    end
+    ln -s $new_src $target
+end
+
+for old in $channel_root/$old_channels
+    rm $old
+end
+
+set --function cmd $current_script_name $argv
+
+if set --query IN_NIX_SHELL
+    eval $cmd
+else
+    nix-shell --run "$cmd"
+end
diff --git a/nix/sources.json b/nix/sources.json
index 9bc46ea0..340505f3 100644
--- a/nix/sources.json
+++ b/nix/sources.json
@@ -1,86 +1,94 @@
 {
-    "agenix": {
-        "branch": "main",
-        "description": "age-encrypted secrets for NixOS and Home manager",
-        "homepage": "https://matrix.to/#/#agenix:nixos.org",
-        "owner": "ryantm",
-        "repo": "agenix",
-        "rev": "24a7ea390564ccd5b39b7884f597cfc8d7f6f44e",
-        "sha256": "165am10r61wl5v4hz169zrlljvj929hgnhr9sn7ak3bz73cr1m86",
-        "type": "tarball",
-        "url": "https://github.com/ryantm/agenix/archive/24a7ea390564ccd5b39b7884f597cfc8d7f6f44e.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    },
-    "emacs-overlay": {
-        "branch": "master",
-        "description": "Bleeding edge emacs overlay [maintainer=@adisbladis] ",
-        "homepage": "",
-        "owner": "nix-community",
-        "repo": "emacs-overlay",
-        "rev": "bb943ccf9c2572550ddfdb7c92373c6671b870af",
-        "sha256": "0mpvgjp3pkr16769kzb16i9yl2lslhv2c743yi91313im76ynnw2",
-        "type": "tarball",
-        "url": "https://github.com/nix-community/emacs-overlay/archive/bb943ccf9c2572550ddfdb7c92373c6671b870af.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    },
-    "home-manager": {
-        "branch": "master",
-        "description": "Manage a user environment using Nix  [maintainer=@rycee] ",
-        "homepage": "https://nix-community.github.io/home-manager/",
-        "owner": "nix-community",
-        "repo": "home-manager",
-        "rev": "c1609d584a6b5e9e6a02010f51bd368cb4782f8e",
-        "sha256": "112r86p3iah1xahwlp82yd3gvh10wkf271za5h7v3jsqv08c6gkr",
-        "type": "tarball",
-        "url": "https://github.com/nix-community/home-manager/archive/c1609d584a6b5e9e6a02010f51bd368cb4782f8e.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    },
-    "nix-darwin": {
-        "branch": "master",
-        "description": "nix modules for darwin",
-        "homepage": "",
-        "owner": "lnl7",
-        "repo": "nix-darwin",
-        "rev": "230a197063de9287128e2c68a7a4b0cd7d0b50a7",
-        "sha256": "17qf4kfxlig7l4vryiirhylkms583ybvy81s1xwdlbah0jpgkklm",
-        "type": "tarball",
-        "url": "https://github.com/lnl7/nix-darwin/archive/230a197063de9287128e2c68a7a4b0cd7d0b50a7.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    },
-    "nix-index-database": {
-        "branch": "main",
-        "description": "Weekly updated nix-index database [maintainer=@Mic92]",
-        "homepage": "",
-        "owner": "Mic92",
-        "repo": "nix-index-database",
-        "rev": "941c4973c824509e0356be455d89613611f76c8a",
-        "sha256": "0nicnyj8gqjv2102dpf0xkv41b6dxcldgyf3gfzj6jfckikiwskf",
-        "type": "tarball",
-        "url": "https://github.com/Mic92/nix-index-database/archive/941c4973c824509e0356be455d89613611f76c8a.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    },
-    "nixos-hardware": {
-        "branch": "master",
-        "description": "A collection of NixOS modules covering hardware quirks.",
-        "homepage": "",
-        "owner": "NixOS",
-        "repo": "nixos-hardware",
-        "rev": "53db5e1070d07e750030bf65f1b9963df8f0c678",
-        "sha256": "0imqm7yz0p75k06ic810nwv5ml1nm8mdbr1fg51qgigvn7i1ckcy",
-        "type": "tarball",
-        "url": "https://github.com/NixOS/nixos-hardware/archive/53db5e1070d07e750030bf65f1b9963df8f0c678.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    },
-    "nixpkgs": {
-        "branch": "nixos-unstable",
-        "description": "Nix Packages collection & NixOS",
-        "homepage": "",
-        "owner": "NixOS",
-        "repo": "nixpkgs",
-        "rev": "58a1abdbae3217ca6b702f03d3b35125d88a2994",
-        "sha256": "05rpyikxpjf15nrzggrx4m0kjkjcmpyhm714nfrbhsp5cp1x1m4r",
-        "type": "tarball",
-        "url": "https://github.com/NixOS/nixpkgs/archive/58a1abdbae3217ca6b702f03d3b35125d88a2994.tar.gz",
-        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
-    }
+  "agenix": {
+    "branch": "main",
+    "description": "age-encrypted secrets for NixOS and Home manager",
+    "homepage": "https://matrix.to/#/#agenix:nixos.org",
+    "owner": "ryantm",
+    "repo": "agenix",
+    "rev": "24a7ea390564ccd5b39b7884f597cfc8d7f6f44e",
+    "sha256": "165am10r61wl5v4hz169zrlljvj929hgnhr9sn7ak3bz73cr1m86",
+    "type": "tarball",
+    "url": "https://github.com/ryantm/agenix/archive/24a7ea390564ccd5b39b7884f597cfc8d7f6f44e.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "emacs-overlay": {
+    "branch": "master",
+    "description": "Bleeding edge emacs overlay [maintainer=@adisbladis] ",
+    "homepage": "",
+    "owner": "nix-community",
+    "repo": "emacs-overlay",
+    "rev": "bb943ccf9c2572550ddfdb7c92373c6671b870af",
+    "sha256": "0mpvgjp3pkr16769kzb16i9yl2lslhv2c743yi91313im76ynnw2",
+    "type": "tarball",
+    "url": "https://github.com/nix-community/emacs-overlay/archive/bb943ccf9c2572550ddfdb7c92373c6671b870af.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "home-manager": {
+    "branch": "master",
+    "description": "Manage a user environment using Nix  [maintainer=@rycee] ",
+    "homepage": "https://nix-community.github.io/home-manager/",
+    "owner": "nix-community",
+    "repo": "home-manager",
+    "rev": "c1609d584a6b5e9e6a02010f51bd368cb4782f8e",
+    "sha256": "112r86p3iah1xahwlp82yd3gvh10wkf271za5h7v3jsqv08c6gkr",
+    "type": "tarball",
+    "url": "https://github.com/nix-community/home-manager/archive/c1609d584a6b5e9e6a02010f51bd368cb4782f8e.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "darwin": {
+    "branch": "master",
+    "description": "nix modules for darwin",
+    "homepage": "",
+    "owner": "lnl7",
+    "repo": "nix-darwin",
+    "rev": "230a197063de9287128e2c68a7a4b0cd7d0b50a7",
+    "sha256": "17qf4kfxlig7l4vryiirhylkms583ybvy81s1xwdlbah0jpgkklm",
+    "type": "tarball",
+    "url": "https://github.com/lnl7/nix-darwin/archive/230a197063de9287128e2c68a7a4b0cd7d0b50a7.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "nix-index-database": {
+    "branch": "main",
+    "description": "Weekly updated nix-index database [maintainer=@Mic92]",
+    "homepage": "",
+    "owner": "Mic92",
+    "repo": "nix-index-database",
+    "rev": "941c4973c824509e0356be455d89613611f76c8a",
+    "sha256": "0nicnyj8gqjv2102dpf0xkv41b6dxcldgyf3gfzj6jfckikiwskf",
+    "type": "tarball",
+    "url": "https://github.com/Mic92/nix-index-database/archive/941c4973c824509e0356be455d89613611f76c8a.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "nixos-hardware": {
+    "branch": "master",
+    "description": "A collection of NixOS modules covering hardware quirks.",
+    "homepage": "",
+    "owner": "NixOS",
+    "repo": "nixos-hardware",
+    "rev": "53db5e1070d07e750030bf65f1b9963df8f0c678",
+    "sha256": "0imqm7yz0p75k06ic810nwv5ml1nm8mdbr1fg51qgigvn7i1ckcy",
+    "type": "tarball",
+    "url": "https://github.com/NixOS/nixos-hardware/archive/53db5e1070d07e750030bf65f1b9963df8f0c678.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "nixpkgs": {
+    "branch": "nixos-unstable",
+    "description": "Nix Packages collection & NixOS",
+    "homepage": "",
+    "owner": "NixOS",
+    "repo": "nixpkgs",
+    "rev": "58a1abdbae3217ca6b702f03d3b35125d88a2994",
+    "sha256": "05rpyikxpjf15nrzggrx4m0kjkjcmpyhm714nfrbhsp5cp1x1m4r",
+    "type": "tarball",
+    "url": "https://github.com/NixOS/nixpkgs/archive/58a1abdbae3217ca6b702f03d3b35125d88a2994.tar.gz",
+    "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+  },
+  "nixpkgs-overlays": {
+    "path": "overlays",
+    "type": "local"
+  },
+  "private": {
+    "path": "private",
+    "type": "local"
+  }
 }
diff --git a/nixos-rebuild b/nixos-rebuild
index 856a8b68..2eaccdef 120000
--- a/nixos-rebuild
+++ b/nixos-rebuild
@@ -1 +1 @@
-/Users/alan/projects/alanpearce.eu/nixfiles/home-manager
\ No newline at end of file
+home-manager
\ No newline at end of file
diff --git a/shell.nix b/shell.nix
index a0eb33c8..9167cb7e 100644
--- a/shell.nix
+++ b/shell.nix
@@ -1,13 +1,30 @@
-let
+with builtins;
 
+let
   sources = import ./nix/sources.nix;
+  sourcesJSON = fromJSON (readFile ./nix/sources.json);
 
   nixpkgs = sources.nixpkgs;
-
   pkgs = import nixpkgs { };
-
+  inherit (pkgs.lib) path attrsets;
+
+  mkPathable = v: toString (
+    if
+      path.subpath.isValid v
+    then
+      path.append ./. v
+    else
+      v
+  );
+
+  nixPath = concatStringsSep ":" (
+    attrsets.mapAttrsToList
+      (k: v: "${k}=${mkPathable (getAttr "outPath" v)}")
+      (intersectAttrs sourcesJSON sources)
+  );
 in
-pkgs.mkShell rec {
+pkgs.mkShell
+{
 
   name = "nixfiles-shell";
 
@@ -19,9 +36,7 @@ pkgs.mkShell rec {
   ];
 
   shellHook = ''
-    export HOST=$(hostname)
-    export NIX_PATH="nixpkgs=${nixpkgs}:home-manager=${sources."home-manager"}:darwin=${sources.nix-darwin}:darwin-config=$PWD/system/$HOST.nix:private=$PWD/private:nixpkgs-overlays=$PWD/overlays:nix-index-database=${sources.nix-index-database}";
-    export HOME_MANAGER_CONFIG="./user/$HOST.nix"
+    export NIX_PATH="${nixPath}";
   '';
 
 }
diff --git a/system/settings/darwin.nix b/system/settings/darwin.nix
index aa55cce5..b6a4fac7 100644
--- a/system/settings/darwin.nix
+++ b/system/settings/darwin.nix
@@ -32,6 +32,11 @@
     allowUnfree = true;
   };
 
+  # needed so that nix-darwin can activate the system as root
+  security.sudo.extraConfig = ''
+    Defaults	env_keep += "NIX_PATH"
+  '';
+
   launchd.user.agents.lorri = lib.mkIf config.services.lorri.enable {
     serviceConfig = {
       RunAtLoad = lib.mkForce false;