Skip to content

Commit

Permalink
refactor: align architecture with srid/nixos-config (#7)
Browse files Browse the repository at this point in the history
* feat: add minimal just file

- see https://just.systems/man/en/

* refactor: add default.nix to home

* feat: add starship config from srid/nixos-config

* refactor: migrate terminal config flake -> ./home

* refactor: update flake to account for refactoring

- use `systems.url = "github:nix-systems/default";`
- migrate default, terminal, starship configs to ./home
- set default username to "runner" for compatibility with debugging in github actions

* refactor: update flake.lock

* fix: remove disabled autojump deferring to zoxide

- #7 (comment)

* fix: remove asciinema

- #7 (comment)

* fix: specify formatter in flake

- #7 (comment)

* fix: fully qualify references to all inputs

- #7 (comment)

* feat: add run target that refs lint and check

* refactor: remove unused home.nix

* fix: update relative path to neovim in comment

* feat: io/update/build/clean targets for justfile

* fix: point to nix-systems/default in comment

- #7 (comment)

* fix: justfile indent spacing

* refactor: make `./home` a home-manager module

... rather than a flake-parts module
- #7 (comment)
- #7 (comment)
- #7 (comment)

* feat: add direnv to use flake

* Add a devshell (since there is .envrc)

* Simplify justfile default

* docs: update README

* fix: lint gitignore

* chore: update flake lock

* feat: add manual dev recipe to justfile

* fix: spacing typo in README

---------

Co-authored-by: Sridhar Ratnakumar <srid@srid.ca>
  • Loading branch information
cameronraysmith and srid committed Sep 10, 2023
1 parent a5491ed commit 049e533
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 60 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
result
.direnv
58 changes: 39 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nix-dev-home

A [`home-manager`](https://github.com/nix-community/home-manager) template providing useful tools &amp; 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 &amp; 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

Expand All @@ -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

<details>

<summary>nix-dev-env setup</summary>

```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] ❯
```

</details>

- 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.
42 changes: 29 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
Expand All @@ -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 ];
};
};
};
}
9 changes: 9 additions & 0 deletions home/default.nix
Original file line number Diff line number Diff line change
@@ -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.
];
}
23 changes: 23 additions & 0 deletions home/starship.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
};
}
56 changes: 35 additions & 21 deletions home.nix → home/terminal.nix
Original file line number Diff line number Diff line change
@@ -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 <pat>` to cd to some directory
zoxide.enable = true;
# Type `<ctrl> + 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;
Expand All @@ -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 <pat>` to cd to some directory
zoxide.enable = true;
};
}
40 changes: 40 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 049e533

Please sign in to comment.