diff --git a/default.nix b/default.nix index e3648d41..44e7ea22 100644 --- a/default.nix +++ b/default.nix @@ -73,4 +73,6 @@ in inherit (eval) options config; system = eval.config.system.build.toplevel; + + installer = pkgs.callPackage ./pkgs/darwin-installer {}; } diff --git a/pkgs/darwin-installer/configuration.nix b/pkgs/darwin-installer/configuration.nix new file mode 100644 index 00000000..7cf87a81 --- /dev/null +++ b/pkgs/darwin-installer/configuration.nix @@ -0,0 +1,44 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + imports = [ ]; + + system.activationScripts.preUserActivation.text = mkBefore '' + darwinPath=$(NIX_PATH=${concatStringsSep ":" config.nix.nixPath} nix-instantiate --eval -E '' 2> /dev/null) || true + if ! test -e "$darwinPath"; then + if test -t 1; then + read -p "Would you like to manage with nix-channel? [y/n] " i + fi + case "$i" in + y|Y) + nix-channel --add https://github.com/LnL7/nix-darwin/archive/master.tar.gz darwin + nix-channel --update + ;; + esac + fi + + if ! test -L /etc/bashrc && ! grep -q /etc/static/bashrc /etc/bashrc; then + if test -t 1; then + read -p "Would you like to load darwin configuration in /etc/bashrc? [y/n] " i + fi + case "$i" in + y|Y) + echo 'if test -e /etc/static/bashrc; then . /etc/static/bashrc; fi' | sudo tee -a /etc/bashrc + ;; + esac + fi + + if ! test -L /etc/profile && grep -q 'etc/profile.d/nix-daemon.sh' /etc/profile; then + if test -t 1; then + read -p "Would you like to remove nix-daemon.sh configuration in /etc/profile? [y/n] " i + fi + case "$i" in + y|Y) + sudo patch -d /etc -p1 < '${./profile.patch}' + ;; + esac + fi + ''; +} diff --git a/pkgs/darwin-installer/default.nix b/pkgs/darwin-installer/default.nix new file mode 100644 index 00000000..058a679a --- /dev/null +++ b/pkgs/darwin-installer/default.nix @@ -0,0 +1,50 @@ +{ stdenv, nix, pkgs }: + +let + nixPath = stdenv.lib.concatStringsSep ":" [ + "darwin-config=${toString ./configuration.nix}" + "darwin=${toString ../..}" + "nixpkgs=${toString pkgs.path}" + "$NIX_PATH" + ]; +in + +stdenv.mkDerivation { + name = "darwin-installer"; + + unpackPhase = ":"; + + installPhase = '' + mkdir -p $out/bin + echo "$shellHook" > $out/bin/darwin-installer + chmod +x $out/bin/darwin-installer + ''; + + shellHook = '' + #!/usr/bin/env bash + set -e + + export nix=${nix} + + config=$(nix-instantiate --eval -E '' 2> /dev/null || echo "$HOME/.nixpkgs/darwin-configuration.nix") + if ! test -f "$config"; then + echo "copying example configuration.nix" >&2 + mkdir -p "$HOME/.nixpkgs" + cp "${toString ../../modules/examples/simple.nix}" "$config" + fi + + export NIX_PATH=${nixPath} + system=$($nix/bin/nix-build '' -I "user-darwin-config=$config" -A system --no-out-link) + export PATH=$system/sw/bin:$PATH + + darwin-rebuild switch -I "user-darwin-config=$config" + + echo >&2 + echo " Open '$config' to get started." >&2 + echo " See the README for more information: https://github.com/LnL7/nix-darwin/blob/master/README.md" >&2 + echo >&2 + echo " Don't forget to start a new shell or source /etc/static/bashrc." >&2 + echo >&2 + exit + ''; +} diff --git a/pkgs/darwin-installer/profile.patch b/pkgs/darwin-installer/profile.patch new file mode 100644 index 00000000..a9453ed5 --- /dev/null +++ b/pkgs/darwin-installer/profile.patch @@ -0,0 +1,14 @@ +diff --git a/profile b/profile +index 3748e3b..36ad2cd 100644 +--- a/profile ++++ b/profile +@@ -7,9 +7,3 @@ fi + if [ "${BASH-no}" != "no" ]; then + [ -r /etc/bashrc ] && . /etc/bashrc + fi +- +-# Nix +-if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then +- . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' +-fi +-# End Nix