Skip to content

Instantly share code, notes, and snippets.

@shinzui
Forked from jmatsushita/README
Created July 7, 2022 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinzui/8b0efcef727aec2677c597dc992379db to your computer and use it in GitHub Desktop.
Save shinzui/8b0efcef727aec2677c597dc992379db to your computer and use it in GitHub Desktop.
Setup nix, nix-darwin and home-manager from scratch on an M1 Macbook Pro
# I found some good resources but they seem to do a bit too much (maybe from a time when there were more bugs).
# So here's a minimal Gist which worked for me as an install on a new M1 Pro.
# Inspired by https://github.com/malob/nixpkgs I highly recommend looking at malob's repo for a more thorough configuration
#
# Let's get started
#
# Let's install nix (at the time of writing this is version 2.5.1
curl -L https://nixos.org/nix/install | sh
# I might not have needed to, but I rebooted
mkdir -p ~/.config/nix
# Emable nix-command and flakes to bootstrap
cat <<EOF > ~/.config/nix/nix.conf
experimental-features = nix-command flakes
EOF
# Get the flake.nix in this gist
cd ~/.config
curl https://gist.githubusercontent.com/jmatsushita/5c50ef14b4b96cb24ae5268dab613050/raw/24a755065de59fc77a552518e106454750e86a49/flake.nix -O
# Get the configuration.nix and home.nix
curl https://gist.githubusercontent.com/jmatsushita/5c50ef14b4b96cb24ae5268dab613050/raw/24a755065de59fc77a552518e106454750e86a49/configuration.nix -O
curl https://gist.githubusercontent.com/jmatsushita/5c50ef14b4b96cb24ae5268dab613050/raw/24a755065de59fc77a552518e106454750e86a49/home.nix -O
# Until this is addressed https://github.com/LnL7/nix-darwin/issues/149
sudo mv /etc/nix/nix.conf /etc/nix/.nix-darwin.bkp.nix.conf
# Build the configuration
nix build .#darwinConfigurations.j-one.system
./result/sw/bin/darwin-rebuild switch --flake .
# Enjoy!
# Might be useful to install x86 packages in the nix profile manually
nix profile install nixpkgs#legacyPackages.x86_64-darwin.haskellPackages.stack
{ pkgs, lib, ... }:
{
# Nix configuration ------------------------------------------------------------------------------
nix.binaryCaches = [
"https://cache.nixos.org/"
];
nix.binaryCachePublicKeys = [
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
];
nix.trustedUsers = [
"@admin"
];
users.nix.configureBuildUsers = true;
# Enable experimental nix command and flakes
# nix.package = pkgs.nixUnstable;
nix.extraOptions = ''
auto-optimise-store = true
experimental-features = nix-command flakes
'' + lib.optionalString (pkgs.system == "aarch64-darwin") ''
extra-platforms = x86_64-darwin aarch64-darwin
'';
# Create /etc/bashrc that loads the nix-darwin environment.
programs.zsh.enable = true;
# Auto upgrade nix package and the daemon service.
services.nix-daemon.enable = true;
# Apps
# `home-manager` currently has issues adding them to `~/Applications`
# Issue: https://github.com/nix-community/home-manager/issues/1341
environment.systemPackages = with pkgs; [
kitty
terminal-notifier
];
# https://github.com/nix-community/home-manager/issues/423
environment.variables = {
TERMINFO_DIRS = "${pkgs.kitty.terminfo.outPath}/share/terminfo";
};
programs.nix-index.enable = true;
# Fonts
fonts.enableFontDir = true;
fonts.fonts = with pkgs; [
recursive
(nerdfonts.override { fonts = [ "JetBrainsMono" ]; })
];
# Keyboard
system.keyboard.enableKeyMapping = true;
system.keyboard.remapCapsLockToEscape = true;
# Add ability to used TouchID for sudo authentication
security.pam.enableSudoTouchIdAuth = true;
}
{
description = "Jun's darwin system";
inputs = {
# Package sets
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-21.11-darwin";
nixpkgs-unstable.url = github:NixOS/nixpkgs/nixpkgs-unstable;
# Environment/system management
darwin.url = "github:lnl7/nix-darwin/master";
darwin.inputs.nixpkgs.follows = "nixpkgs-unstable";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs-unstable";
# Other sources
comma = { url = github:Shopify/comma; flake = false; };
};
outputs = { self, darwin, nixpkgs, home-manager, ... }@inputs:
let
inherit (darwin.lib) darwinSystem;
inherit (inputs.nixpkgs-unstable.lib) attrValues makeOverridable optionalAttrs singleton;
# Configuration for `nixpkgs`
nixpkgsConfig = {
config = { allowUnfree = true; };
overlays = attrValues self.overlays ++ singleton (
# Sub in x86 version of packages that don't build on Apple Silicon yet
final: prev: (optionalAttrs (prev.stdenv.system == "aarch64-darwin") {
inherit (final.pkgs-x86)
idris2
nix-index
niv
purescript;
})
);
};
in
{
# My `nix-darwin` configs
darwinConfigurations = rec {
j-one = darwinSystem {
system = "aarch64-darwin";
modules = attrValues self.darwinModules ++ [
# Main `nix-darwin` config
./configuration.nix
# `home-manager` module
home-manager.darwinModules.home-manager
{
nixpkgs = nixpkgsConfig;
# `home-manager` config
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.users.jun = import ./home.nix;
}
];
};
};
# Overlays --------------------------------------------------------------- {{{
overlays = {
# Overlays to add various packages into package set
comma = final: prev: {
comma = import inputs.comma { inherit (prev) pkgs; };
};
# Overlay useful on Macs with Apple Silicon
apple-silicon = final: prev: optionalAttrs (prev.stdenv.system == "aarch64-darwin") {
# Add access to x86 packages system is running Apple Silicon
pkgs-x86 = import inputs.nixpkgs-unstable {
system = "x86_64-darwin";
inherit (nixpkgsConfig) config;
};
};
};
# My `nix-darwin` modules that are pending upstream, or patched versions waiting on upstream
# fixes.
darwinModules = {
programs-nix-index =
# Additional configuration for `nix-index` to enable `command-not-found` functionality with Fish.
{ config, lib, pkgs, ... }:
{
config = lib.mkIf config.programs.nix-index.enable {
programs.fish.interactiveShellInit = ''
function __fish_command_not_found_handler --on-event="fish_command_not_found"
${if config.programs.fish.useBabelfish then ''
command_not_found_handle $argv
'' else ''
${pkgs.bashInteractive}/bin/bash -c \
"source ${config.programs.nix-index.package}/etc/profile.d/command-not-found.sh; command_not_found_handle $argv"
''}
end
'';
};
};
security-pam =
# Upstream PR: https://github.com/LnL7/nix-darwin/pull/228
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.security.pam;
# Implementation Notes
#
# We don't use `environment.etc` because this would require that the user manually delete
# `/etc/pam.d/sudo` which seems unwise given that applying the nix-darwin configuration requires
# sudo. We also can't use `system.patchs` since it only runs once, and so won't patch in the
# changes again after OS updates (which remove modifications to this file).
#
# As such, we resort to line addition/deletion in place using `sed`. We add a comment to the
# added line that includes the name of the option, to make it easier to identify the line that
# should be deleted when the option is disabled.
mkSudoTouchIdAuthScript = isEnabled:
let
file = "/etc/pam.d/sudo";
option = "security.pam.enableSudoTouchIdAuth";
in ''
${if isEnabled then ''
# Enable sudo Touch ID authentication, if not already enabled
if ! grep 'pam_tid.so' ${file} > /dev/null; then
sed -i "" '2i\
auth sufficient pam_tid.so # nix-darwin: ${option}
' ${file}
fi
'' else ''
# Disable sudo Touch ID authentication, if added by nix-darwin
if grep '${option}' ${file} > /dev/null; then
sed -i "" '/${option}/d' ${file}
fi
''}
'';
in
{
options = {
security.pam.enableSudoTouchIdAuth = mkEnableOption ''
Enable sudo authentication with Touch ID
When enabled, this option adds the following line to /etc/pam.d/sudo:
auth sufficient pam_tid.so
(Note that macOS resets this file when doing a system update. As such, sudo
authentication with Touch ID won't work after a system update until the nix-darwin
configuration is reapplied.)
'';
};
config = {
system.activationScripts.extraActivation.text = ''
# PAM settings
echo >&2 "setting up pam..."
${mkSudoTouchIdAuthScript cfg.enableSudoTouchIdAuth}
'';
};
};
};
};
}
{ config, pkgs, lib, ... }:
{
home.stateVersion = "22.05";
# https://github.com/malob/nixpkgs/blob/master/home/default.nix
# Direnv, load and unload environment variables depending on the current directory.
# https://direnv.net
# https://rycee.gitlab.io/home-manager/options.html#opt-programs.direnv.enable
programs.direnv.enable = true;
programs.direnv.nix-direnv.enable = true;
# Htop
# https://rycee.gitlab.io/home-manager/options.html#opt-programs.htop.enable
programs.htop.enable = true;
programs.htop.settings.show_program_path = true;
home.packages = with pkgs; [
# Some basics
coreutils
curl
wget
# Dev stuff
# (agda.withPackages (p: [ p.standard-library ]))
google-cloud-sdk
haskellPackages.cabal-install
haskellPackages.hoogle
haskellPackages.hpack
haskellPackages.implicit-hie
haskellPackages.stack
idris2
jq
nodePackages.typescript
nodejs
purescript
# Useful nix related tools
cachix # adding/managing alternative binary caches hosted by Cachix
# comma # run software from without installing it
niv # easy dependency management for nix projects
nodePackages.node2nix
] ++ lib.optionals stdenv.isDarwin [
cocoapods
m-cli # useful macOS CLI commands
];
# Misc configuration files --------------------------------------------------------------------{{{
# https://docs.haskellstack.org/en/stable/yaml_configuration/#non-project-specific-config
home.file.".stack/config.yaml".text = lib.generators.toYAML {} {
templates = {
scm-init = "git";
params = {
author-name = "Your Name"; # config.programs.git.userName;
author-email = "youremail@example.com"; # config.programs.git.userEmail;
github-username = "yourusername";
};
};
nix.enable = true;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment