diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index b2be92b..726d2d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ result +.direnv diff --git a/README.md b/README.md index 00aeba5..d97d6f4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # nix-dev-home -A [`home-manager`](https://github.com/nix-community/home-manager) template providing useful tools & settings for Nix-based development. Neovim pre-configured for Haskell Language Server is included as a key demonstration (try it with [haskell-template](https://github.com/srid/haskell-template).) +A [`home-manager`](https://github.com/nix-community/home-manager) template providing useful tools & settings for Nix-based development. Neovim pre-configured for Haskell Language Server is included as a key demonstration (try it with [haskell-template](https://github.com/srid/haskell-template)). ## Usage @@ -14,31 +14,51 @@ nix flake init -t github:juspay/nix-dev-home nix flake update # Update inputs to use latest software ``` -(If you do not already have it, [install Nix](https://haskell.flake.page/nix) first) +(If you do not already have it, [install Nix](https://zero-to-flakes.com/install) first) -This will create a `flake.nix` file and a `home.nix` in the current directory. +This will create a `flake.nix` file in the current directory and a `./home` directory containing the home-manager configuration that you can review starting with `./home/default.nix`. It will also create a [justfile](https://github.com/casey/just), which provides a set of recipes analogous to Make targets to interact with the nix flake. -- Edit `home.nix` to your liking (see [configuration example](https://nix-community.github.io/home-manager/index.html#sec-usage-configuration)). -- Also, you must change the user name in `flake.nix` from `john` to your actual user name. -- You can then run `nix run` to activate this configuration in your $HOME. - - After running this, restart your terminal. Expect to see the [starship](https://starship.rs/) prompt. When you `cd` into a project containing `.envrc` configured for flakes (such as [haskell-template](https://github.com/srid/haskell-template)), you should be automatically be put in the `nix develop` shell along with a change to the starship prompt indicating the same. - -To browse the capabilities of home-manager (and to see what else can go in your `home.nix` -- such as shell aliases), consult https://nix-community.github.io/home-manager/options.html +> [!NOTE] +> Executing commands below will modify the contents of your `$HOME` directory. You will be [warned before overwriting](https://nix-community.github.io/home-manager/index.html#sec-usage-dotfiles), but not before creating links to newly created configuration files in the nix store. Since home-manager does not currently provide an integrated and automated feature to eliminate the links it creates, be aware that if you would like to reverse this operation, you will need to curate your home directory manually. + +- Edit the contents of `./home` to your liking (see [configuration example](https://nix-community.github.io/home-manager/index.html#sec-usage-configuration)). +- You must also change the user name in `flake.nix` from `runner` to your actual user name. `runner` is used by default for compatibility with [github actions](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables). +- You can then execute `nix develop`, to ensure you are in the development shell with [just](https://github.com/casey/just) installed, followed by `just run` to activate this configuration in your `$HOME`. On most systems you are likely to experience at least one of the issues mentioned below in [Troubleshooting](#troubleshooting). A more complete sequence might be + +
+ + nix-dev-env setup + + ```sh + > nix develop + (nix:nix-dev-home-env) > rm ~/.bashrc ~/.profile && just run && direnv allow + (nix:nix-dev-home-env) > exit + > bash + runner on 12ca6a64c923 work on  feature/branch via ❄️ impure (nix-dev-home-env) + ⬢ [Docker] ❯ + ``` + +
+ +- If you prefer, you can simply execute `nix run`, but using `just` will perform some additional validation and ensure you are able to use the other commands in the [justfile](./justfile). + - After running this, restart your terminal. Expect to see the [starship](https://starship.rs/) prompt. When you `cd` into a project containing `.envrc` configured for flakes (such as [haskell-template](https://github.com/srid/haskell-template)), you should automatically be put in the `nix develop` shell along with a change to the starship prompt indicating the same. + +To browse the capabilities of home-manager (and to see what else can go in your `./home` -- such as shell aliases), consult [https://nix-community.github.io/home-manager/options.html](https://nix-community.github.io/home-manager/options.html). ## Troubleshooting -- Running`nix run` (home-manager) complains `"Existing file ... is in the way of ..."` - - Delete those existing dotfiles, and try again. In home-manager, you can configure your shell directly in Nix (for macOS zsh, this is [`programs.zsh.envExtra`](https://nix-community.github.io/home-manager/options.html#opt-programs.zsh.envExtra)). -- Cannot use cachix: Running `nix run nixpkgs#cachix use nammayatri` (for example) does not succeed. - - Add yourself to the trusted-users list and restart your macOS machine. - ```sh - mkdir -p ~/.config/nix - echo "trusted-users = root $USER" > $HOME/.config/nix/nix.conf - ``` +- Running `nix run` (home-manager) complains `"Existing file ... is in the way of ..."` + - Delete those existing dotfiles, and try again. In home-manager, you can configure your shell directly in Nix (for macOS zsh, this is [`programs.zsh.envExtra`](https://nix-community.github.io/home-manager/options.html#opt-programs.zsh.envExtra)). + - Cannot use cachix: Running `nix run nixpkgs#cachix use nammayatri` (for example) does not succeed. + - Add yourself to the trusted-users list and restart your macOS machine. + + ```sh + mkdir -p ~/.config/nix + echo "trusted-users = root $USER" > $HOME/.config/nix/nix.conf + ``` ## FAQ ### But I use NixOS -You can embed this configuration inside your NixOS configuration, and thus share it with non-NixOS systems (like macOS and Ubuntu). See the "both" template of https://github.com/srid/nixos-flake for an example. If you don't want to share the configuration with macOS (ie., you use only Linux for development), see the "linux" template instead. - +You can embed this configuration inside your NixOS configuration, and thus share it with non-NixOS systems (like macOS and Ubuntu). See the "both" template of [https://github.com/srid/nixos-flake](https://github.com/srid/nixos-flake) for an example. If you don't want to share the configuration with macOS (ie., you use only Linux for development), see the "linux" template instead. diff --git a/flake.lock b/flake.lock index 95d5b39..711d52f 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "nixpkgs-lib": "nixpkgs-lib" }, "locked": { - "lastModified": 1690933134, - "narHash": "sha256-ab989mN63fQZBFrkk4Q8bYxQCktuHmBIBqUG1jl6/FQ=", + "lastModified": 1693611461, + "narHash": "sha256-aPODl8vAgGQ0ZYFIRisxYG5MOGSkIczvu2Cd8Gb9+1Y=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "59cf3f1447cfc75087e7273b04b31e689a8599fb", + "rev": "7f53fdb7bdc5bb237da7fefef12d099e4fd611ca", "type": "github" }, "original": { @@ -25,11 +25,11 @@ ] }, "locked": { - "lastModified": 1692763155, - "narHash": "sha256-qMrGKZ8c/q/mHO3ZdrcBPwiVVXPLLgXjY98Ejqb5kAA=", + "lastModified": 1694134858, + "narHash": "sha256-fG/ESauOGmiojKlpJG8gB62dJa5Wd+ZIuiDMKK/HD3g=", "owner": "nix-community", "repo": "home-manager", - "rev": "6a20e40acaebf067da682661aa67da8b36812606", + "rev": "19c6a4081b14443420358262f8416149bd79561a", "type": "github" }, "original": { @@ -55,11 +55,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1692734709, - "narHash": "sha256-SCFnyHCyYjwEmgUsHDDuU0TsbVMKeU1vwkR+r7uS2Rg=", + "lastModified": 1694183432, + "narHash": "sha256-YyPGNapgZNNj51ylQMw9lAgvxtM2ai1HZVUu3GS8Fng=", "owner": "nixos", "repo": "nixpkgs", - "rev": "b85ed9dcbf187b909ef7964774f8847d554fab3b", + "rev": "db9208ab987cdeeedf78ad9b4cf3c55f5ebd269b", "type": "github" }, "original": { @@ -72,11 +72,11 @@ "nixpkgs-lib": { "locked": { "dir": "lib", - "lastModified": 1690881714, - "narHash": "sha256-h/nXluEqdiQHs1oSgkOOWF+j8gcJMWhwnZ9PFabN6q0=", + "lastModified": 1693471703, + "narHash": "sha256-0l03ZBL8P1P6z8MaSDS/MvuU8E75rVxe5eE1N6gxeTo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "9e1960bc196baf6881340d53dccb203a951745a2", + "rev": "3e52e76b70d5508f3cec70b882a29199f4d1ee85", "type": "github" }, "original": { @@ -92,7 +92,23 @@ "flake-parts": "flake-parts", "home-manager": "home-manager", "nixos-flake": "nixos-flake", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "systems": "systems" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 765f138..a4fa954 100644 --- a/flake.nix +++ b/flake.nix @@ -1,4 +1,6 @@ { + description = "A home-manager template providing useful tools & settings for Nix-based development"; + inputs = { # Principle inputs (updated by `nix run .#update`) nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; @@ -7,17 +9,18 @@ flake-parts.url = "github:hercules-ci/flake-parts"; nixos-flake.url = "github:srid/nixos-flake"; + + # see https://github.com/nix-systems/default/blob/main/default.nix + systems.url = "github:nix-systems/default"; }; - outputs = inputs@{ self, ... }: + outputs = inputs: inputs.flake-parts.lib.mkFlake { inherit inputs; } { - systems = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin" ]; + systems = import inputs.systems; imports = [ inputs.nixos-flake.flakeModule ]; - flake.homeModules.default = ./home.nix; - flake.templates.default = { description = "A `home-manager` template providing useful tools & settings for Nix-based development"; path = builtins.path { @@ -31,25 +34,36 @@ perSystem = { self', pkgs, ... }: let # TODO: Change username - myUserName = "john"; + myUserName = "runner"; in { legacyPackages.homeConfigurations.${myUserName} = - self.nixos-flake.lib.mkHomeConfiguration + inputs.self.nixos-flake.lib.mkHomeConfiguration pkgs ({ pkgs, ... }: { - imports = [ self.homeModules.default ]; + # Edit the contents of the ./home directory to install packages and modify dotfile configuration in your + # $HOME. + # + # https://nix-community.github.io/home-manager/index.html#sec-usage-configuration + imports = [ ./home ]; home.username = myUserName; home.homeDirectory = "/${if pkgs.stdenv.isDarwin then "Users" else "home"}/${myUserName}"; home.stateVersion = "22.11"; }); + formatter = pkgs.nixpkgs-fmt; + # Enables 'nix run' to activate. apps.default.program = self'.packages.activate-home; # Enable 'nix build' to build the home configuration, but without # activating. packages.default = self'.legacyPackages.homeConfigurations.${myUserName}.activationPackage; + + devShells.default = pkgs.mkShell { + name = "nix-dev-home"; + nativeBuildInputs = with pkgs; [ just ]; + }; }; }; } diff --git a/home/default.nix b/home/default.nix new file mode 100644 index 0000000..16d6e7c --- /dev/null +++ b/home/default.nix @@ -0,0 +1,9 @@ +{ + imports = [ + # This loads ./neovim/default.nix - neovim configured for Haskell dev, and other things. + ./neovim + ./starship.nix + ./terminal.nix + # Add more of your home-manager modules here. + ]; +} diff --git a/home/starship.nix b/home/starship.nix new file mode 100644 index 0000000..9258ef6 --- /dev/null +++ b/home/starship.nix @@ -0,0 +1,23 @@ +{ + # Starship configuration from this example: + # https://github.com/srid/nixos-config/blob/f9cf0def19fbc7aa1e836be481ce50d214e34036/home/starship.nix#L4-L19 + programs.starship = { + enable = true; + settings = { + username = { + style_user = "blue bold"; + style_root = "red bold"; + format = "[$user]($style) "; + disabled = false; + show_always = true; + }; + hostname = { + ssh_only = false; + ssh_symbol = "🌐 "; + format = "on [$hostname](bold red) "; + trim_at = ".local"; + disabled = false; + }; + }; + }; +} diff --git a/home.nix b/home/terminal.nix similarity index 67% rename from home.nix rename to home/terminal.nix index 846f7aa..a4ab884 100644 --- a/home.nix +++ b/home/terminal.nix @@ -1,29 +1,50 @@ -# Edit this to install packages and modify dotfile configuration in your -# $HOME. -# -# https://nix-community.github.io/home-manager/index.html#sec-usage-configuration -{ pkgs, ... }: { - imports = [ - # This loads ./home/neovim/default.nix - neovim configured for Haskell dev, and other things. - ./home/neovim - # Add more of your home-manager modules here. - ]; +{ pkgs, ... }: +# Platform-independent terminal setup +{ # Nix packages to install to $HOME # # Search for packages here: https://search.nixos.org/packages home.packages = with pkgs; [ - tmate - nix-info - cachix - lazygit # Better git UI + # Unix tools ripgrep # Better `grep` + fd + sd + tree + + # Nix dev + cachix nil # Nix language server + nix-info + nixpkgs-fmt nixci + + # Dev + gh + just + lazygit # Better git UI + tmate ]; + home.shellAliases = rec { + e = "nvim"; + g = "git"; + lg = "lazygit"; + t = "tree"; + }; + # Programs natively supported by home-manager. programs = { + bat.enable = true; + # Type `z ` to cd to some directory + zoxide.enable = true; + # Type ` + r` to fuzzy search your shell history + fzf.enable = true; + jq.enable = true; + nix-index.enable = true; + htop.enable = true; + + # on macOS, you probably don't need this bash = { enable = true; @@ -47,12 +68,5 @@ enable = true; nix-direnv.enable = true; }; - - # Want to configure starship? See this example: - # https://github.com/srid/nixos-config/blob/f9cf0def19fbc7aa1e836be481ce50d214e34036/home/starship.nix#L4-L19 - starship.enable = true; - - # Type `z ` to cd to some directory - zoxide.enable = true; }; } diff --git a/justfile b/justfile new file mode 100644 index 0000000..0c30464 --- /dev/null +++ b/justfile @@ -0,0 +1,40 @@ +# Like GNU `make`, but `just` rustier. +# https://just.systems/man/en/chapter_19.html +# run `just` from this directory to see available commands + +# Default command when 'just' is run without arguments +default: + @just --list + +# Print nix flake inputs and outputs +io: + nix flake metadata + nix flake show + +# Update nix flake +update: + nix flake update + +# Lint nix files +lint: + nix fmt + +# Check nix flake +check: + nix flake check + +# Manually enter dev shell +dev: + nix develop + +# Build nix flake +build: lint check + nix build + +# Remove build output link (no garbage collection) +clean: + rm -f ./result + +# Run nix flake to setup environment +run: lint check + nix run