home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

bootstrap.sh (3702B)


      1 #!/usr/bin/env bash
      2 # This scripts aims to detect which system is running, and bootstrap
      3 # the home configuration accordingly. So far the current setup are
      4 # supported:
      5 # - NixOS (>= 19.03 more or less)
      6 # - Fedora (>= 30)
      7 # - Mac OS X (>= 10.14)
      8 
      9 set -e
     10 
     11 # Install nix
     12 setup_nix() {
     13     echo "> Install nix"
     14     curl https://nixos.org/nix/install | sh
     15 }
     16 
     17 # Install home-manager (without running it)
     18 setup_home-manager() {
     19     echo "> Install home-manager"
     20     mkdir -m 0755 -p /nix/var/nix/{profiles,gcroots}/per-user/$USER
     21     nix-channel --add https://github.com/rycee/home-manager/archive/master.tar.gz home-manager
     22     nix-channel --update
     23 }
     24 
     25 run_home-manager() {
     26     nix-shell '<home-manager>' -A install
     27 }
     28 
     29 # Fedora is "managed" by nix directly
     30 # - bootstrap nix + home-manager
     31 setup_nixos() {
     32     echo "NixOS detected"
     33     setup_home-manager
     34     run_home-manager
     35 }
     36 
     37 # Fedora is "managed" mainly using ansible
     38 # - install ansible
     39 # - play the "correct" playbook
     40 # - boostrap nix + home-manager
     41 setup_fedora() {
     42     echo "Fedora detected"
     43     if hash nix 2>/dev/null; then
     44 	echo "> nix already present"
     45     else
     46 	setup_nix
     47 	echo "if [ -e $HOME/.nix-profile/etc/profile.d/nix.sh ]; then . $HOME/.nix-profile/etc/profile.d/nix.sh; fi # added by Nix installer" >> $HOME/.bashrc
     48 	. $HOME/.bashrc
     49     fi
     50     if hash home-manager 2>/dev/null; then
     51 	echo "> home-manager already present"
     52     else
     53 	setup_home-manager
     54 	echo "export NIX_PATH=$HOME/.nix-defexpr/channels\${NIX_PATH:+:}\$NIX_PATH" >> $HOME/.bashrc
     55     fi
     56     if [[ ! -f $HOME/.config/nixpkgs/home.nix ]]; then
     57        echo "> create a temporary home-manager configuration"
     58        mkdir -p $HOME/.config/nixpkgs/
     59        cat > $HOME/.config/nixpkgs/home.nix <<EOF
     60 {
     61   programs.home-manager.enable = true;
     62   programs.man.enable = false;
     63   home.extraOutputsToInstall = [ "man" ];
     64 }
     65 
     66 EOF
     67     fi
     68     echo "> setup nix caches"
     69     mkdir -p $HOME/.config/nix/
     70     cat > $HOME/.config/nix/nix.conf <<EOF
     71 substituters = http://nix.cache.home https://cache.nixos.org https://shortbrain.cachix.org
     72 trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= shortbrain.cachix.org-1:dqXcXzM0yXs3eo9ChmMfmob93eemwNyhTx7wCR4IjeQ=
     73 EOF
     74     run_home-manager
     75     echo ". \"$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh\"" >> $HOME/.bashrc
     76     . $HOME/.bashrc
     77     dnf copr enable evana/fira-code-fonts
     78     dnf install fira-code-fonts
     79     echo "> install ansible"
     80     sudo dnf install -y ansible
     81     echo "> run playbook"
     82     ansible-playbook -K ansible/playbook.yml
     83 }
     84 
     85 setup_osx() {
     86     echo "Mac OS X detected"
     87     if [[ "$kernel_name" == "Darwin" ]]; then
     88         IFS=$'\n' read -d "" -ra sw_vers < <(awk -F'<|>' '/key|string/ {print $3}' \
     89                             "/System/Library/CoreServices/SystemVersion.plist")
     90         for ((i=0;i<${#sw_vers[@]};i+=2)) {
     91 		case ${sw_vers[i]} in
     92                     ProductName)          darwin_name=${sw_vers[i+1]} ;;
     93                     ProductVersion)       osx_version=${sw_vers[i+1]} ;;
     94                     ProductBuildVersion)  osx_build=${sw_vers[i+1]}   ;;
     95 		esac
     96             }
     97      fi
     98 }
     99 
    100 IFS=" " read -ra uname <<< "$(uname -srm)"
    101 kernel_name="${uname[0]}"
    102 kernel_version="${uname[1]}"
    103 kernel_machine="${uname[2]}"
    104 
    105 case "$kernel_name" in
    106     "Linux" | "GNU")
    107 	if [[ -f "/etc/os-release" || -f "/usr/lib/os-release" ]]; then
    108                 files=("/etc/os-release" "/usr/lib/os-release")
    109 
    110                 # Source the os-release file
    111                 for file in "${files[@]}"; do
    112                     source "$file" && break
    113                 done
    114 		case "$ID" in
    115 		    "nixos")
    116 			setup_nixos ;;
    117 		    "fedora")
    118 			setup_fedora ;;
    119 		esac
    120 	fi ;;
    121     "Darwin")
    122 	setup_osx ;;
    123 esac