From 431a4cdc06d3c48ac6ef6160bc684687a5f0a80a Mon Sep 17 00:00:00 2001 From: Dich0tomy Date: Mon, 27 May 2024 18:17:12 +0000 Subject: [PATCH] chore: modularize flake (#1440) --- flake.nix | 183 ++---------------------------- nix/checks/default.nix | 34 ++++++ nix/overlays/default.nix | 28 +++++ nix/overlays/lib.nix | 12 ++ nix/overlays/luarc.nix | 35 ++++++ nix/packages/default.nix | 3 + nix/packages/integration-test.nix | 110 ++++++++++++++++++ nix/shells/default.nix | 17 +++ 8 files changed, 250 insertions(+), 172 deletions(-) create mode 100644 nix/checks/default.nix create mode 100644 nix/overlays/default.nix create mode 100644 nix/overlays/lib.nix create mode 100644 nix/overlays/luarc.nix create mode 100644 nix/packages/default.nix create mode 100644 nix/packages/integration-test.nix create mode 100644 nix/shells/default.nix diff --git a/flake.nix b/flake.nix index dd45125f8..df746a262 100644 --- a/flake.nix +++ b/flake.nix @@ -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"; @@ -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 + ]; }; }; } diff --git a/nix/checks/default.nix b/nix/checks/default.nix new file mode 100644 index 000000000..4c01f3e3d --- /dev/null +++ b/nix/checks/default.nix @@ -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; + }; + }; + }; + }; +} diff --git a/nix/overlays/default.nix b/nix/overlays/default.nix new file mode 100644 index 000000000..50846de75 --- /dev/null +++ b/nix/overlays/default.nix @@ -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;}; + }) + ]; + }; + }; +} diff --git a/nix/overlays/lib.nix b/nix/overlays/lib.nix new file mode 100644 index 000000000..ee013b2f1 --- /dev/null +++ b/nix/overlays/lib.nix @@ -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; +} diff --git a/nix/overlays/luarc.nix b/nix/overlays/luarc.nix new file mode 100644 index 000000000..902c2cec4 --- /dev/null +++ b/nix/overlays/luarc.nix @@ -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/"]; + }; + } diff --git a/nix/packages/default.nix b/nix/packages/default.nix new file mode 100644 index 000000000..c8c6faf3e --- /dev/null +++ b/nix/packages/default.nix @@ -0,0 +1,3 @@ +{pkgs, ...}: { + packages.integration-test = pkgs.callPackage ./integration-test.nix {}; +} diff --git a/nix/packages/integration-test.nix b/nix/packages/integration-test.nix new file mode 100644 index 000000000..dc11c9ce2 --- /dev/null +++ b/nix/packages/integration-test.nix @@ -0,0 +1,110 @@ +{ + writeScript, + writeShellApplication, + neovim-unwrapped, + tree-sitter, + lua5_1, + wget, +}: let + kickstart-config = + 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 + writeShellApplication { + name = "neorg-integration-test"; + + runtimeInputs = [ + 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 + ''; + } diff --git a/nix/shells/default.nix b/nix/shells/default.nix new file mode 100644 index 000000000..53a8557db --- /dev/null +++ b/nix/shells/default.nix @@ -0,0 +1,17 @@ +{pkgs, ...}: { + devShells.default = pkgs.mkShell { + name = "neorg devShell"; + + shellHook = '' + ln -fs ${pkgs.luarc-to-json pkgs.luarc-with-dependencies} .luarc.json + ''; + + packages = with pkgs; [ + lua-language-server + stylua + lua51Packages.luacheck + nil + lua5_1 + ]; + }; +}