Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize flake #1440

Merged
merged 11 commits into from
May 27, 2024
183 changes: 11 additions & 172 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# General TODOS:
# - Extract into modules for better readability
# - Add comments explaining the more terse parts of the flake.
{
description = "Flake for Neorg development and testing";
Expand Down Expand Up @@ -29,181 +28,21 @@
"x86_64-darwin"
"aarch64-darwin"
];
perSystem = {
config,
self',
inputs',
pkgs,
system,
lib,
...
}: let
pkgs = import nixpkgs {
inherit system;
overlays = [
gen-luarc.overlays.default
];
};
dependencies = builtins.fromJSON (builtins.readFile ./res/deps.json);
install-dependencies =
pkgs.runCommand "install-neorg-dependencies" {
nativeBuildInputs = with pkgs; [lua51Packages.luarocks wget];
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = "sha256-SOsIgtmkXTKMZrKUHHzAf+XAshl/J7+DN9RFeLz+DDY=";
} ''
mkdir $PWD/home
export HOME=$PWD/home
mkdir -p $out/luarocks

${lib.concatStrings (lib.mapAttrsToList (name: version: ''luarocks install --tree="$out/luarocks" --force-lock --local ${name} ${version}'' + "\n") dependencies)}
'';
luarc = pkgs.mk-luarc {};
luarc-with-dependencies =
luarc
// {
inherit (luarc) runtime;
inherit (luarc.Lua) diagnostics globals;
Lua.workspace = {
inherit (luarc.Lua.workspace) ignoreDir;
library = luarc.Lua.workspace.library ++ ["${install-dependencies}/luarocks/share/lua/5.1/"];
};
};
in {
formatter = pkgs.alejandra;

checks.type-check = git-hooks.lib.${system}.run {
src = ./lua;
hooks = {
lua-ls = {
enable = true;
settings.configuration = luarc-with-dependencies;
};
};
};

checks.pre-commit-check = git-hooks.lib.${system}.run {
src = self;
hooks = {
alejandra.enable = true;
luacheck.enable = true;
# stylua.enable = true;
};
};

packages.integration-test = let
kickstart-config =
pkgs.writeScript "kickstart.lua"
''
-- Adapted from https://github.com/folke/lazy.nvim#-installation

-- Install lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)

-- Set up both the traditional leader (for keymaps) as well as the local leader (for norg files)
vim.g.mapleader = " "
vim.g.maplocalleader = ","

require("lazy").setup({
{
"rebelot/kanagawa.nvim", -- neorg needs a colorscheme with treesitter support
config = function()
vim.cmd.colorscheme("kanagawa")
end,
},
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
opts = {
ensure_installed = { "c", "lua", "vim", "vimdoc", "query" },
highlight = { enable = true },
},
config = function(_, opts)
require("nvim-treesitter.configs").setup(opts)
end,
},
{
"vhyrro/luarocks.nvim",
priority = 1000,
config = true,
},
{
"nvim-neorg/neorg",
dependencies = { "luarocks.nvim" },
config = function()
require("neorg").setup {
load = {
["core.defaults"] = {},
["core.concealer"] = {},
["core.dirman"] = {
config = {
workspaces = {
notes = "~/notes",
},
default_workspace = "notes",
},
},
},
}

vim.cmd.e("success")

vim.wo.foldlevel = 99
vim.wo.conceallevel = 2
end,
}
})
'';
in
pkgs.writeShellApplication {
name = "neorg-integration-test";
_module.args = {inherit gen-luarc git-hooks;};

runtimeInputs = with pkgs; [neovim-unwrapped tree-sitter lua5_1 wget kickstart-config];

text = ''
export NVIM_APPNAME="nvim-neorg"

echo "* Hello World!" > example.norg

nvim --headless -u ${kickstart-config} example.norg -c wq

rm example.norg

if [ ! -f success ]; then
echo "Integration test failed!"
exit 1
fi

rm success
'';
};

devShells.default = pkgs.mkShell {
name = "neorg devShell";
imports = [
./nix/overlays
./nix/checks
];

shellHook = ''
ln -fs ${pkgs.luarc-to-json luarc-with-dependencies} .luarc.json
'';
perSystem = {pkgs, ...}: {
formatter = pkgs.alejandra;

packages = with pkgs; [
lua-language-server
stylua
lua51Packages.luacheck
nil
lua5_1
];
};
imports = [
./nix/packages
./nix/shells
];
};
};
}
34 changes: 34 additions & 0 deletions nix/checks/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
self,
git-hooks,
...
}: {
perSystem = {
pkgs,
system,
...
}: {
checks = {
type-check = git-hooks.lib.${system}.run {
src = "${self}/lua";

hooks = {
lua-ls = {
enable = true;
settings.configuration = pkgs.luarc-with-dependencies;
};
};
};

pre-commit-check = git-hooks.lib.${system}.run {
src = "${self}";

hooks = {
alejandra.enable = true;
luacheck.enable = true;
# stylua.enable = true;
};
};
};
};
}
28 changes: 28 additions & 0 deletions nix/overlays/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
self,
inputs,
gen-luarc,
...
}: {
perSystem = {
pkgs,
system,
...
}: {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;

overlays = [
gen-luarc.overlays.default

(final: prev: {
lib = prev.lib // import ./lib.nix {inherit pkgs;};

# NOTE: I would have used callPackage for easy overriding, but
# this changes the type and *-to-json fails later. To be figured out.
luarc-with-dependencies = final.lib.callPackageNoOverridable ./luarc.nix {inherit self;};
})
];
};
};
}
12 changes: 12 additions & 0 deletions nix/overlays/lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{pkgs}: rec {
callPackageNoOverridableWith = originalAttrs: fun: additionalAttrs: let
f =
if pkgs.lib.isFunction fun
then fun
else import fun;
attrs = builtins.intersectAttrs (pkgs.lib.functionArgs f) originalAttrs;
in
f (attrs // additionalAttrs);

callPackageNoOverridable = callPackageNoOverridableWith pkgs;
}
35 changes: 35 additions & 0 deletions nix/overlays/luarc.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
lib,
self,
mk-luarc,
runCommand,
lua51Packages,
wget,
}: let
luarc = mk-luarc {};

dependencies = builtins.fromJSON (builtins.readFile "${self}/res/deps.json");

install-dependencies =
runCommand "install-neorg-dependencies" {
nativeBuildInputs = [lua51Packages.luarocks wget];
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = "sha256-SOsIgtmkXTKMZrKUHHzAf+XAshl/J7+DN9RFeLz+DDY=";
} ''
mkdir $PWD/home
export HOME=$PWD/home
mkdir -p $out/luarocks

${lib.concatStrings (lib.mapAttrsToList (name: version: ''luarocks install --tree="$out/luarocks" --force-lock --local ${name} ${version} '' + "\n") dependencies)}
'';
in
luarc
// {
inherit (luarc) runtime;
inherit (luarc.Lua) diagnostics globals;
Lua.workspace = {
inherit (luarc.Lua.workspace) ignoreDir;
library = luarc.Lua.workspace.library ++ ["${install-dependencies}/luarocks/share/lua/5.1/"];
};
}
3 changes: 3 additions & 0 deletions nix/packages/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{pkgs, ...}: {
packages.integration-test = pkgs.callPackage ./integration-test.nix {};
}
Loading
Loading