Experimental arbitrary directory dotfiles install + re-added simplified phoenix wrapper script + updated readme with these changes

This commit is contained in:
Emmet 2024-04-14 09:40:46 -05:00
parent 3148ea5ab7
commit 7c457d29de
14 changed files with 189 additions and 46 deletions

View file

@ -118,8 +118,7 @@
user = home-manager.lib.homeManagerConfiguration {
inherit pkgs;
modules = [
(./. + "/profiles" + ("/" + systemSettings.profile)
+ "/home.nix") # load home.nix from selected PROFILE
(./. + "/profiles" + ("/" + systemSettings.profile) + "/home.nix") # load home.nix from selected PROFILE
];
extraSpecialArgs = {
# pass config variables from above
@ -136,8 +135,8 @@
system = lib.nixosSystem {
system = systemSettings.system;
modules = [
(./. + "/profiles" + ("/" + systemSettings.profile)
+ "/configuration.nix")
(./. + "/profiles" + ("/" + systemSettings.profile) + "/configuration.nix")
./system/bin/phoenix.nix
]; # load configuration.nix from selected PROFILE
specialArgs = {
# pass config variables from above

View file

@ -14,11 +14,11 @@
# After running this, the command `nix flake update` will require root
if [ "$#" = 1 ]; then
dotfilesDir=$1;
SCRIPT_DIR=$1;
else
dotfilesDir=$(pwd);
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
fi
pushd $dotfilesDir &> /dev/null;
pushd $SCRIPT_DIR &> /dev/null;
chown 0:0 .;
chown 0:0 profiles/*;
chown -R 0:0 system;

View file

@ -21,6 +21,12 @@ nix run github:librephoenix/nixos-config
nix run git+https://codeberg.org/librephoenix/nixos-config
#+END_SRC
This will install the dotfiles to =~/.dotfiles=, but if you'd like to install to a custom directory, just supply it as a positional argument, i.e:
#+BEGIN_SRC sh :noeval
# Install from gitlab
nix run gitlab:librephoenix/nixos-config /your/custom/directory
#+END_SRC
The script will ask for sudo permissions at certain points, /but you should not run the script as root/.
If the above =nix run= command gives you an error, odds are you either don't have =git= installed, or you haven't enabled the experimental features in your Nix config (=nix-command= and =flakes=). To get the command to install properly, you can first enter a shell with =git= available using:
@ -37,6 +43,11 @@ And if you want a single copy-paste solution:
nix-shell -p git --command "nix run --experimental-features 'nix-command flakes' gitlab:librephoenix/nixos-config"
#+end_src
This /should/ still work with a custom dotfiles directory too, i.e:
#+begin_src sh :noeval
nix-shell -p git --command "nix run --experimental-features 'nix-command flakes' gitlab:librephoenix/nixos-config /your/custom/directory"
#+end_src
At a certain point in the install script it will open =nano= (or whatever your $EDITOR is set to) and ask you to edit the =flake.nix=. You can edit as much or as little of the config variables as you like, and it will continue the install after you exit the editor.
Potential Errors: I've only tested it working on UEFI with the default EFI mount point of =/boot=. I've added experimental legacy (BIOS) boot support, but it does rely on a quick and dirty script to find the grub device. If you are testing it using some weird boot configuration for whatever reason, try modifying =bootMountPath= (UEFI) or =grubDevice= (legacy BIOS) in =flake.nix= before install, or else it will complain about not being able to install the bootloader.
@ -53,6 +64,13 @@ The dotfiles can be installed after cloning the repo into =~/.dotfiles= using:
git clone https://gitlab.com/librephoenix/nixos-config.git ~/.dotfiles
~/.dotfiles/install.sh
#+END_SRC
or with a custom directory:
#+BEGIN_SRC sh :noeval
git clone https://gitlab.com/librephoenix/nixos-config.git /your/custom/directory
/your/custom/directory/install.sh
#+END_SRC
If you install to a custom directory, make sure to edit the =userSettings.dotfilesDir= in the [[./flake.nix][flake.nix]], or else my [[./system/bin/phoenix.nix][phoenix wrapper script]] won't work.
At a certain point in the install script it will open =nano= (or whatever your =$EDITOR= is set to) and ask you to edit the =flake.nix=. You can edit as much or as little of the config variables as you like, and it will continue the install after you exit the editor.
@ -83,6 +101,22 @@ Start by cloning the repo:
git clone https://gitlab.com/librephoenix/nixos-config.git ~/.dotfiles
#+END_SRC
Any custom directory should also work:
#+BEGIN_SRC sh :noeval
git clone https://gitlab.com/librephoenix/nixos-config.git /your/custom/directory
/your/custom/directory/install.sh
#+END_SRC
If you install to a custom directory, make sure to edit the =userSettings.dotfilesDir= in the beginning [[./flake.nix][flake.nix]], or else my [[./system/bin/phoenix.nix][phoenix wrapper script]] won't work.
#+BEGIN_SRC nix :noeval
...
let
...
# ----- USER SETTINGS ----- #
dotfilesDir = "/your/custom/directory"; # username
...
#+END_SRC
To get the hardware configuration on a new system, either copy from =/etc/nixos/hardware-configuration.nix= or run:
#+BEGIN_SRC sh :noeval
sudo nixos-generate-config --show-hardware-config > ~/.dotfiles/system/hardware-configuration.nix
@ -106,10 +140,10 @@ The build will fail if you are booting from BIOS instead of UEFI, unless change
...
let
# ---- SYSTEM SETTINGS ---- #
systemSettings = {
...
bootMode = "bios"; # uefi or bios
grubDevice = "/dev/vda"; # device identifier for grub; find this by running lsblk
};
...
#+end_src
Note: If you're installing this to a VM, Hyprland won't work unless 3D acceleration is enabled.
@ -123,12 +157,20 @@ Once the variables are set, then switch into the system configuration by running
#+BEGIN_SRC sh :noeval
sudo nixos-rebuild switch --flake ~/.dotfiles#system
#+END_SRC
or for your own custom directory:
#+BEGIN_SRC sh :noeval
sudo nixos-rebuild switch --flake /your/custom/directory#system
#+END_SRC
*** Intall and Switch Home Manager Config
Home manager can be installed and the configuration activated with:
#+BEGIN_SRC sh :noeval
nix run home-manager/master -- switch --flake ~/.dotfiles#user
#+END_SRC
or for your own custom directory:
#+BEGIN_SRC sh :noeval
nix run home-manager/master -- switch --flake /your/custom/directory#user
#+END_SRC
** FAQ
*** =home-manager switch --flake .#user= Command Fails

View file

@ -5,37 +5,43 @@
# Clone dotfiles
# TODO make ~/.dotfiles path arbitrary and make all other scripts conform to this
# using SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
nix-shell -p git --command "git clone https://gitlab.com/librephoenix/nixos-config ~/.dotfiles"
if [ -z "$1" ]
then
SCRIPT_DIR=$1
else
SCRIPT_DIR=~/.dotfiles
fi
nix-shell -p git --command "git clone https://gitlab.com/librephoenix/nixos-config $SCRIPT_DIR"
# Generate hardware config for new system
sudo nixos-generate-config --show-hardware-config > ~/.dotfiles/system/hardware-configuration.nix
sudo nixos-generate-config --show-hardware-config > $SCRIPT_DIR/system/hardware-configuration.nix
# Check if uefi or bios
if [ -d /sys/firmware/efi/efivars ]; then
sed -i "0,/bootMode.*=.*\".*\";/s//bootMode = \"uefi\";/" ~/.dotfiles/flake.nix
sed -i "0,/bootMode.*=.*\".*\";/s//bootMode = \"uefi\";/" $SCRIPT_DIR/flake.nix
else
sed -i "0,/bootMode.*=.*\".*\";/s//bootMode = \"bios\";/" ~/.dotfiles/flake.nix
sed -i "0,/bootMode.*=.*\".*\";/s//bootMode = \"bios\";/" $SCRIPT_DIR/flake.nix
grubDevice=$(findmnt / | awk -F' ' '{ print $2 }' | sed 's/\[.*\]//g' | tail -n 1 | lsblk -no pkname | tail -n 1 )
sed -i "0,/grubDevice.*=.*\".*\";/s//grubDevice = \"\/dev\/$grubDevice\";/" ~/.dotfiles/flake.nix
sed -i "0,/grubDevice.*=.*\".*\";/s//grubDevice = \"\/dev\/$grubDevice\";/" $SCRIPT_DIR/flake.nix
fi
# Patch flake.nix with different username/name and remove email by default
sed -i "0,/emmet/s//$(whoami)/" ~/.dotfiles/flake.nix
sed -i "0,/Emmet/s//$(getent passwd $(whoami) | cut -d ':' -f 5 | cut -d ',' -f 1)/" ~/.dotfiles/flake.nix
sed -i "s/emmet@librephoenix.com//" ~/.dotfiles/flake.nix
sed -i "0,/emmet/s//$(whoami)/" $SCRIPT_DIR/flake.nix
sed -i "0,/Emmet/s//$(getent passwd $(whoami) | cut -d ':' -f 5 | cut -d ',' -f 1)/" $SCRIPT_DIR/flake.nix
sed -i "s/emmet@librephoenix.com//" $SCRIPT_DIR/flake.nix
sed -i "s+~/.dotfiles+$SCRIPT_DIR+g" $SCRIPT_DIR/flake.nix
# Open up editor to manually edit flake.nix before install
if [ -z "$EDITOR" ]; then
EDITOR=nano;
fi
$EDITOR ~/.dotfiles/flake.nix;
$EDITOR $SCRIPT_DIR/flake.nix;
# Permissions for files that should be owned by root
sudo ~/.dotfiles/harden.sh ~/.dotfiles;
sudo $SCRIPT_DIR/harden.sh $SCRIPT_DIR;
# Rebuild system
sudo nixos-rebuild switch --flake ~/.dotfiles#system;
sudo nixos-rebuild switch --flake $SCRIPT_DIR#system;
# Install and build home-manager configuration
nix run home-manager/master --extra-experimental-features nix-command --extra-experimental-features flakes -- switch --flake ~/.dotfiles#user;
nix run home-manager/master --extra-experimental-features nix-command --extra-experimental-features flakes -- switch --flake $SCRIPT_DIR#user;

15
pull.sh
View file

@ -1,21 +1,20 @@
#!/bin/sh
# Automated script to update my non-primary systems
# to be in sync with upstream git repo while
# config to be in sync with upstream git repo while
# preserving local edits to dotfiles via git stash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Relax permissions temporarily so git can work
sudo ~/.dotfiles/soften.sh ~/.dotfiles;
sudo $SCRIPT_DIR/soften.sh $SCRIPT_DIR;
# Stash local edits, pull changes, and re-apply local edits
pushd ~/.dotfiles;
pushd $SCRIPT_DIR &> /dev/null;
git stash;
git pull;
git stash apply;
popd;
popd &> /dev/null;
# Permissions for files that should be owned by root
sudo ~/.dotfiles/harden.sh ~/.dotfiles;
# Synchronize system
~/.dotfiles/sync.sh;
sudo $SCRIPT_DIR/harden.sh $SCRIPT_DIR;

View file

@ -18,10 +18,10 @@
# RUNNING nixos-rebuild switch!
if [ "$#" = 1 ]; then
dotfilesDir=$1;
SCRIPT_DIR=$1;
else
dotfilesDir=$(pwd);
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
fi
pushd $dotfilesDir &> /dev/null;
pushd $SCRIPT_DIR &> /dev/null;
chown -R 1000:users .;
popd &> /dev/null;

10
sync-system.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/sh
# Script to synchronize system state
# with configuration files for nixos system
# and home-manager
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Rebuild system
sudo nixos-rebuild switch --flake $SCRIPT_DIR#system;

12
sync-user.sh Executable file
View file

@ -0,0 +1,12 @@
#!/bin/sh
# Script to synchronize system state
# with configuration files for nixos system
# and home-manager
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Install and build home-manager configuration
home-manager switch --flake $SCRIPT_DIR#user;
$SCRIPT_DIR/sync-posthook.sh

View file

@ -4,10 +4,7 @@
# with configuration files for nixos system
# and home-manager
# Rebuild system
sudo nixos-rebuild switch --flake ~/.dotfiles#system;
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Install and build home-manager configuration
home-manager switch --flake ~/.dotfiles#user;
~/.dotfiles/sync-posthook.sh
$SCRIPT_DIR/sync-system.sh
$SCRIPT_DIR/sync-user.sh

View file

@ -11,6 +11,8 @@ imports = [ import1.nix
My system-level Nix modules are organized into this directory:
- [[./hardware-configuration.nix][hardware-configuration]] - Default hardware config generated for my system
- [[./bin][bin]] - My own scripts
- [[./bin/phoenix.nix][phoenix]] - My nix command wrapper
- [[./app][app]] - Necessary system-level configuration to get various apps working
- [[./hardware][hardware]] - Hardware configurations I may need to use
- [[./security][security]] - System-level security stuff
@ -18,6 +20,7 @@ My system-level Nix modules are organized into this directory:
- [[./wm][wm]] - Necessary system-level configuration to get various window managers, wayland compositors, and/or desktop environments working
** Variables imported from flake.nix
# TODO update this
Variables can be imported from [[../flake.nix][flake.nix]] by setting the =specialArgs= block inside the flake (see [[../flake.nix][my flake]] for more details). This allows variables to merely be managed in one place ([[../flake.nix][flake.nix]]) rather than having to manage them in multiple locations.
I currently import the following variables to the system config:

73
system/bin/phoenix.nix Normal file
View file

@ -0,0 +1,73 @@
{ pkgs, userSettings, ... }:
let myScript = ''
if [ "$1" = "sync" ]; then
if [ "$#" = 1 ]; then
''+userSettings.dotfilesDir+''/sync.sh;
exit 0;
elif [ "$2" = "user" ]; then
''+userSettings.dotfilesDir+''/sync-user.sh;
exit 0;
elif [ "$2" = "system" ]; then
''+userSettings.dotfilesDir+''/sync-system.sh;
exit 0;
else
echo "Please pass 'system' or 'user' if supplying a second argument"
fi
elif [ "$1" = "refresh" ]; then
if [ "$#" -gt 1 ]; then
echo "Warning: The 'refresh' command has no subcommands (no $2 subcommand)";
fi
''+userSettings.dotfilesDir+''/sync-posthook.sh;
exit 0;
elif [ "$1" = "update" ]; then
if [ "$#" -gt 1 ]; then
echo "Warning: The 'update' command has no subcommands (no $2 subcommand)";
fi
''+userSettings.dotfilesDir+''/update.sh;
exit 0;
elif [ "$1" = "upgrade" ]; then
if [ "$#" -gt 1 ]; then
echo "Warning: The 'update' command has no subcommands (no $2 subcommand)";
fi
''+userSettings.dotfilesDir+''/upgrade.sh;
exit 0;
elif [ "$1" = "pull" ]; then
if [ "$#" -gt 1 ]; then
echo "Warning: The 'upgrade' command has no subcommands (no $2 subcommand)";
fi
''+userSettings.dotfilesDir+''/pull.sh;
exit 0;
elif [ "$1" = "harden" ]; then
if [ "$#" -gt 1 ]; then
echo "Warning: The 'harden' command has no subcommands (no $2 subcommand)";
fi
''+userSettings.dotfilesDir+''/harden.sh;
exit 0;
elif [ "$1" = "soften" ]; then
if [ "$#" -gt 1 ]; then
echo "Warning: The 'soften' command has no subcommands (no $2 subcommand)";
fi
''+userSettings.dotfilesDir+''/soften.sh;
exit 0;
elif [ "$1" = "gc" ]; then
if [ "$#" -gt 2 ]; then
echo "Warning: The 'gc' command only accepts one argument (collect_older_than)";
fi
if [ "$2" = "full" ]; then
sudo nix-collect-garbage --delete-old;
nix-collect-garbage --delete-old;
elif [ "$2" ]; then
sudo nix-collect-garbage --delete-older-than $2;
nix-collect-garbage --delete-older-than $2;
else
sudo nix-collect-garbage --delete-older-than 30d;
nix-collect-garbage --delete-older-than 30d;
fi
fi
'';
in
{
environment.systemPackages = [
(pkgs.writeScriptBin "phoenix" myScript)
];
}

View file

@ -3,5 +3,7 @@
# Script to update my flake without
# synchronizing configuration
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Update flake
sudo nix flake update ~/.dotfiles;
sudo nix flake update $SCRIPT_DIR;

View file

@ -3,8 +3,10 @@
# Script to update system and sync
# Does not pull changes from git
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Update flake
~/.dotfiles/update.sh;
$SCRIPT_DIR/update.sh;
# Synchronize system
~/.dotfiles/sync.sh;
$SCRIPT_DIR/sync.sh;

View file

@ -21,14 +21,11 @@ My user-level Nix modules are organized into this directory:
- [[./app/ranger][ranger]]
- [[./app/terminal][terminal]] - Configuration for terminal emulators
- [[./app/virtualization][virtualization]] - Virtualization and compatability layers
- [[./bin][bin]] - My own scripts
- [[./bin/phoenix.nix][phoenix]] - My nix command wrapper
- [[./lang][lang]] - Various bundled programming languages
- I will probably get rid of this in favor of a shell.nix for every project, once I learn how that works
- [[./pkgs][pkgs]] - "Package builds" for packages not in the Nix repositories
- [[./pkgs/pokemon-colorscripts.nix][pokemon-colorscripts]]
- [[./pkgs/rogauracore.nix][rogauracore]] - not working yet
- [[./pkgs/ytsub.nix][ytsub]]
- [[./shell][shell]] - My default bash and zsh configs
- [[./shell/sh.nix][sh]] - bash and zsh configs
- [[./shell/cli-collection.nix][cli-collection]] - Curated useful CLI utilities
@ -38,6 +35,7 @@ My user-level Nix modules are organized into this directory:
- [[./wm/picom][picom]]
** Variables imported from flake.nix
# TODO update this
Variables can be imported from [[../flake.nix][flake.nix]] by setting the =extraSpecialArgs= block inside the flake (see [[../flake.nix][my flake]] for more details). This allows variables to merely be managed in one place ([[../flake.nix][flake.nix]]) rather than having to manage them in multiple locations.
I currently import the following variables to the system config: