Skip to content

Commit

Permalink
misc: added flake template (nix-community#219)
Browse files Browse the repository at this point in the history
* Added simple flake template

* Added readme to template

* Updated readme to show how to use the template

* Formatting

* removed unused file from template

* Fixed template url and pkgs

* Formatting
  • Loading branch information
Alexnortung authored Mar 3, 2023
1 parent 6e027c4 commit 2258eb8
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 26 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ for a home-manager installation, `inputs.nixvim.nixosModules.nixvim`, for NixOS,
and `inputs.nixvim.nixDarwinModules.nixvim` for nix-darwin.

## Usage
NixVim can be used in four ways: through the home-manager, nix-darwin, and NixOS modules,
and through the `makeNixvim` function. To use the modules, just import the
NixVim can be used in four ways: through the home-manager, nix-darwin, NixOS modules,
and standalone through the `makeNixvim` function. To use the modules, just import the
`nixvim.homeManagerModules.nixvim`, `nixvim.nixDarwinModules.nixvim`, and
`nixvim.nixosModules.nixvim` modules, depending on which system
you're using.
Expand All @@ -100,9 +100,18 @@ If you want to use it standalone, you can use the `makeNixvim` function:
}
```

To get started with a standalone configuration you can use the template by running the following command in an empty directory (recommended):

```
nix flake init --template github:pta2002/nixvim
```

Alternatively if you want a minimal flake to allow building a custom neovim you
can use the following:

<details>
<summary>Minimal flake configuration</summary>

```nix
{
description = "A very basic flake";
Expand Down Expand Up @@ -132,6 +141,8 @@ can use the following:
}
```

</details>

You can then run neovim using `nix run .# -- <file>`. This can be useful to test
config changes easily.

Expand Down
14 changes: 14 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,26 @@
};
};
formatter = pkgs.alejandra;

lib = import ./lib {
inherit pkgs;
inherit (pkgs) lib;
};
});
in
flakeOutput
// {
nixosModules.nixvim = import ./wrappers/nixos.nix modules;
homeManagerModules.nixvim = import ./wrappers/hm.nix modules;
nixDarwinModules.nixvim = import ./wrappers/darwin.nix modules;

templates = let
simple = {
path = ./templates/simple;
description = "A simple nix flake template for getting started with nixvim";
};
in {
default = simple;
};
};
}
30 changes: 30 additions & 0 deletions lib/check.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{pkgs, ...}: {
checkNvim = {
name,
nvim,
...
}:
pkgs.stdenv.mkDerivation {
name = name;

nativeBuildInputs = [nvim];

dontUnpack = true;
# We need to set HOME because neovim will try to create some files
#
# Because neovim does not return an exitcode when quitting we need to check if there are
# errors on stderr
buildPhase = ''
output=$(HOME=$(realpath .) nvim -mn --headless "+q" 2>&1 >/dev/null)
if [[ -n $output ]]; then
echo "ERROR: $output"
exit 1
fi
'';

# If we don't do this nix is not happy
installPhase = ''
mkdir $out
'';
};
}
6 changes: 6 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Args probably only needs pkgs and lib
args: {
# Add all exported modules here
check = import ./check.nix args;
helpers = import ./helpers.nix args;
}
16 changes: 16 additions & 0 deletions templates/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Nixvim template

This template gives you a good starting point for configuring nixvim standalone.

## Configuring

To start configuring, just add or modify the nix files in `./config`. If you add a new configuration file, remember to add it to the [`congig/default.nix`](./config/default.nix) file

## Testing your new configuration

To test your configuration simply run the following command

```
nix run .
```

5 changes: 5 additions & 0 deletions templates/simple/config/bufferline.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
plugins.bufferline = {
enable = true;
};
}
6 changes: 6 additions & 0 deletions templates/simple/config/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
# Import all your configuration modules here
imports = [
./bufferline.nix
];
}
39 changes: 39 additions & 0 deletions templates/simple/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
description = "A nixvim configuration";

inputs = {
nixvim.url = "github:pta2002/nixvim";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = {
nixpkgs,
nixvim,
flake-utils,
...
} @ inputs: let
nixvimLib = nixvim.lib;
config = import ./config; # import the module directly
in
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {inherit system;};
nixvim' = nixvim.legacyPackages.${system};
nvim = nixvim'.makeNixvimWithModule {
inherit pkgs;
module = config;
};
in {
checks = {
# Run `nix check .` to verify that your config is not broken
default = nixvim.lib.${system}.check.checkNvim {
inherit nvim;
name = "A nixvim configuration";
};
};

packages = {
# Lets you run `nix run .` to start nixvim
default = nvim;
};
});
}
28 changes: 4 additions & 24 deletions tests/checks.nix
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
{
makeNixvim,
pkgs,
}: let
} @ args: let
tests = import ./plugins {inherit (pkgs) lib;};
check = import ../lib/check.nix args;
checkConfig = check.checkNvim;
in
# We attempt to build & execute all configurations
builtins.mapAttrs (
name: config: let
nvim = makeNixvim config;
in
pkgs.stdenv.mkDerivation {
name = name;

nativeBuildInputs = [nvim];

dontUnpack = true;
# We need to set HOME because neovim will try to create some files
#
# Because neovim does not return an exitcode when quitting we need to check if there are
# errors on stderr
buildPhase = ''
output=$(HOME=$(realpath .) nvim -mn --headless "+q" 2>&1 >/dev/null)
if [[ -n $output ]]; then
echo "ERROR: $output"
exit 1
fi
'';

# If we don't do this nix is not happy
installPhase = ''
mkdir $out
'';
}
checkConfig {inherit name nvim;}
)
tests

0 comments on commit 2258eb8

Please sign in to comment.