mirror of
https://github.com/dwinkler1/np.git
synced 2026-02-19 22:40:57 -05:00
Add inline comments and extract Lua config for better clarity
Co-authored-by: dwinkler1 <22460147+dwinkler1@users.noreply.github.com>
This commit is contained in:
parent
eaecb56186
commit
496c238a12
3 changed files with 183 additions and 14 deletions
150
templates/rde/REFACTORING.md
Normal file
150
templates/rde/REFACTORING.md
Normal file
|
|
@ -0,0 +1,150 @@
|
||||||
|
# Template Refactoring Summary
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
This document summarizes the refactoring improvements made to the RDE (Research Development Environment) template flake.
|
||||||
|
|
||||||
|
## Changes Made
|
||||||
|
|
||||||
|
### 1. File Structure Reorganization
|
||||||
|
**Before**: Single 688-line `flake.nix` file
|
||||||
|
**After**: Modular structure with 17 files across 5 directories
|
||||||
|
|
||||||
|
```
|
||||||
|
templates/rde/
|
||||||
|
├── flake.nix (261 lines) - Main configuration
|
||||||
|
├── README.md - User documentation
|
||||||
|
├── REFACTORING.md - This file
|
||||||
|
├── overlays/ (5 files)
|
||||||
|
│ ├── r.nix - R package configuration
|
||||||
|
│ ├── python.nix - Python package configuration
|
||||||
|
│ ├── rix.nix - rstats-on-nix integration
|
||||||
|
│ ├── theme.nix - Neovim theme setup
|
||||||
|
│ └── project-scripts.nix - Script wrapper definitions
|
||||||
|
├── hosts/ (5 files)
|
||||||
|
│ ├── default.nix - Merges all host configs
|
||||||
|
│ ├── python.nix - Python command definitions
|
||||||
|
│ ├── julia.nix - Julia command definitions
|
||||||
|
│ ├── r.nix - R command definitions
|
||||||
|
│ └── utils.nix - Utility command definitions
|
||||||
|
├── lib/ (2 files)
|
||||||
|
│ ├── shell-hook.nix - Dev shell welcome message
|
||||||
|
│ └── mini-notify-config.lua - Neovim notification config
|
||||||
|
└── scripts/ (4 files)
|
||||||
|
├── initPython.sh - Python project initialization
|
||||||
|
├── initProject.sh - Project structure setup
|
||||||
|
├── updateDeps.sh - Dependency update script
|
||||||
|
└── activateDevenv.sh - Devenv activation
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Key Improvements
|
||||||
|
|
||||||
|
#### Separation of Concerns
|
||||||
|
- **Config**: Main configuration stays in flake.nix
|
||||||
|
- **Overlays**: Package modifications isolated in overlays/
|
||||||
|
- **Hosts**: Command definitions organized by language in hosts/
|
||||||
|
- **Scripts**: Shell scripts extracted to scripts/ directory
|
||||||
|
- **Helpers**: Utility functions in lib/
|
||||||
|
|
||||||
|
#### Readability
|
||||||
|
- Reduced main file from 688 to 261 lines (62% reduction)
|
||||||
|
- Added strategic comments explaining key sections
|
||||||
|
- Extracted long inline strings to separate files
|
||||||
|
- Grouped related functionality together
|
||||||
|
|
||||||
|
#### Maintainability
|
||||||
|
- Language-specific changes isolated to dedicated files
|
||||||
|
- Easy to add new languages (create new host/overlay files)
|
||||||
|
- Easy to modify scripts without touching Nix code
|
||||||
|
- Clear separation between different concerns
|
||||||
|
|
||||||
|
#### Reusability
|
||||||
|
- Individual overlays can be reused in other projects
|
||||||
|
- Host definitions can be copied/modified independently
|
||||||
|
- Scripts can be tested/modified separately
|
||||||
|
- Modular design allows selective adoption
|
||||||
|
|
||||||
|
### 3. Specific Extractions
|
||||||
|
|
||||||
|
#### Shell Scripts (200+ lines → 4 files)
|
||||||
|
- `initPython.sh`: Python project initialization logic
|
||||||
|
- `initProject.sh`: Directory structure and git setup
|
||||||
|
- `updateDeps.sh`: Dependency update automation
|
||||||
|
- `activateDevenv.sh`: Devenv shell activation
|
||||||
|
|
||||||
|
#### Overlays (100+ lines → 5 files)
|
||||||
|
- `r.nix`: R package management with rix integration
|
||||||
|
- `python.nix`: Python package configuration
|
||||||
|
- `rix.nix`: rstats-on-nix package source
|
||||||
|
- `theme.nix`: Neovim colorscheme handling
|
||||||
|
- `project-scripts.nix`: Script wrapper generation
|
||||||
|
|
||||||
|
#### Host Definitions (200+ lines → 5 files)
|
||||||
|
- `python.nix`: marimo, ipy, py, initPython commands
|
||||||
|
- `julia.nix`: jl, pluto, initJl commands
|
||||||
|
- `r.nix`: R console command
|
||||||
|
- `utils.nix`: initProject, updateDeps, devenv commands
|
||||||
|
- `default.nix`: Merges all host configurations
|
||||||
|
|
||||||
|
#### Helper Functions (40+ lines → 2 files)
|
||||||
|
- `shell-hook.nix`: Dev shell welcome message generation
|
||||||
|
- `mini-notify-config.lua`: Neovim notification filtering
|
||||||
|
|
||||||
|
### 4. Added Documentation
|
||||||
|
|
||||||
|
#### README.md
|
||||||
|
- Overview of template purpose
|
||||||
|
- Directory structure explanation
|
||||||
|
- Benefits of modular design
|
||||||
|
- Configuration instructions
|
||||||
|
- Extension guidelines
|
||||||
|
- Usage examples
|
||||||
|
|
||||||
|
#### Inline Comments
|
||||||
|
- Section headers in flake.nix
|
||||||
|
- Explanation of key configuration blocks
|
||||||
|
- Purpose of each import
|
||||||
|
- Documentation of categories and settings
|
||||||
|
|
||||||
|
### 5. Benefits Achieved
|
||||||
|
|
||||||
|
1. **Maintainability**:
|
||||||
|
- Changes to one language don't affect others
|
||||||
|
- Easy to locate and modify specific functionality
|
||||||
|
- Clear ownership of different components
|
||||||
|
|
||||||
|
2. **Readability**:
|
||||||
|
- Main file is now scannable and understandable
|
||||||
|
- Related code grouped together
|
||||||
|
- Inline documentation guides users
|
||||||
|
|
||||||
|
3. **Testability**:
|
||||||
|
- Scripts can be tested independently
|
||||||
|
- Overlays can be verified in isolation
|
||||||
|
- Smaller files are easier to debug
|
||||||
|
|
||||||
|
4. **Extensibility**:
|
||||||
|
- Clear patterns for adding new languages
|
||||||
|
- Easy to add new commands
|
||||||
|
- Simple to customize per language
|
||||||
|
|
||||||
|
5. **Learning**:
|
||||||
|
- New users can understand the template structure
|
||||||
|
- Examples in each file guide modifications
|
||||||
|
- Documentation explains purpose and usage
|
||||||
|
|
||||||
|
## Migration Guide
|
||||||
|
|
||||||
|
For users of the old template:
|
||||||
|
1. The functionality remains identical
|
||||||
|
2. Configuration in the main config section is the same
|
||||||
|
3. All commands work exactly as before
|
||||||
|
4. To customize, now edit the specific file in the appropriate directory
|
||||||
|
|
||||||
|
## Future Improvements
|
||||||
|
|
||||||
|
Possible future enhancements:
|
||||||
|
- Add validation scripts for configuration
|
||||||
|
- Create unit tests for individual modules
|
||||||
|
- Add more language examples (Go, Rust, etc.)
|
||||||
|
- Create a configuration wizard script
|
||||||
|
- Add CI/CD integration examples
|
||||||
|
|
@ -56,11 +56,13 @@
|
||||||
###################################
|
###################################
|
||||||
|
|
||||||
# Import overlays from separate files
|
# Import overlays from separate files
|
||||||
|
# Each overlay adds specific packages or configurations
|
||||||
rOverlay = import ./overlays/r.nix;
|
rOverlay = import ./overlays/r.nix;
|
||||||
pythonOverlay = import ./overlays/python.nix;
|
pythonOverlay = import ./overlays/python.nix;
|
||||||
rixOverlay = import ./overlays/rix.nix inputs;
|
rixOverlay = import ./overlays/rix.nix inputs;
|
||||||
extraPkgOverlay = import ./overlays/theme.nix config;
|
extraPkgOverlay = import ./overlays/theme.nix config;
|
||||||
projectScriptsOverlay = import ./overlays/project-scripts.nix config;
|
projectScriptsOverlay = import ./overlays/project-scripts.nix config;
|
||||||
|
|
||||||
supportedSystems = [
|
supportedSystems = [
|
||||||
"x86_64-linux"
|
"x86_64-linux"
|
||||||
"aarch64-linux"
|
"aarch64-linux"
|
||||||
|
|
@ -68,6 +70,8 @@
|
||||||
];
|
];
|
||||||
forSystems = nixpkgs.lib.genAttrs supportedSystems;
|
forSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||||
|
|
||||||
|
# Main package configuration
|
||||||
|
# This configures the Neovim environment with language support
|
||||||
projectConfig = forSystems (
|
projectConfig = forSystems (
|
||||||
system: let
|
system: let
|
||||||
inherit (nixCats) utils;
|
inherit (nixCats) utils;
|
||||||
|
|
@ -97,6 +101,7 @@
|
||||||
mkPlugin,
|
mkPlugin,
|
||||||
...
|
...
|
||||||
} @ packageDef: {
|
} @ packageDef: {
|
||||||
|
# Language servers and runtime dependencies
|
||||||
lspsAndRuntimeDeps = {
|
lspsAndRuntimeDeps = {
|
||||||
project = with pkgs; [];
|
project = with pkgs; [];
|
||||||
julia = with pkgs; [julia-bin];
|
julia = with pkgs; [julia-bin];
|
||||||
|
|
@ -104,6 +109,7 @@
|
||||||
r = with pkgs; [rWrapper quarto air-formatter];
|
r = with pkgs; [rWrapper quarto air-formatter];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Plugins that load automatically
|
||||||
startupPlugins = {
|
startupPlugins = {
|
||||||
project = with pkgs.vimPlugins; [pkgs.extraTheme];
|
project = with pkgs.vimPlugins; [pkgs.extraTheme];
|
||||||
gitPlugins = with pkgs.neovimPlugins; [
|
gitPlugins = with pkgs.neovimPlugins; [
|
||||||
|
|
@ -114,6 +120,7 @@
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Plugins that load on-demand
|
||||||
optionalPlugins = {
|
optionalPlugins = {
|
||||||
project = with pkgs.vimPlugins; [];
|
project = with pkgs.vimPlugins; [];
|
||||||
gitPlugins = with pkgs.neovimPlugins; [
|
gitPlugins = with pkgs.neovimPlugins; [
|
||||||
|
|
@ -122,25 +129,14 @@
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Lua code to run before main config
|
||||||
optionalLuaPreInit = {
|
optionalLuaPreInit = {
|
||||||
project = [
|
project = [
|
||||||
''
|
(builtins.readFile ./lib/mini-notify-config.lua)
|
||||||
local predicate = function(notif)
|
|
||||||
if not (notif.data.source == "lsp_progress" and notif.data.client_name == "lua_ls") then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
-- Filter out some LSP progress notifications from 'lua_ls'
|
|
||||||
return notif.msg:find("Diagnosing") == nil and notif.msg:find("semantic tokens") == nil
|
|
||||||
end
|
|
||||||
local custom_sort = function(notif_arr)
|
|
||||||
return MiniNotify.default_sort(vim.tbl_filter(predicate, notif_arr))
|
|
||||||
end
|
|
||||||
require("mini.notify").setup({ content = { sort = custom_sort } })
|
|
||||||
vim.notify = MiniNotify.make_notify()
|
|
||||||
''
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Lua code to run after main config
|
||||||
optionalLuaAdditions = {
|
optionalLuaAdditions = {
|
||||||
project = ["vim.notify('Project loaded: ${name}')"];
|
project = ["vim.notify('Project loaded: ${name}')"];
|
||||||
};
|
};
|
||||||
|
|
@ -149,6 +145,7 @@
|
||||||
project = {};
|
project = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Environment variables for each language
|
||||||
environmentVariables = {
|
environmentVariables = {
|
||||||
project = {};
|
project = {};
|
||||||
julia = {JULIA_NUM_THREADS = "auto";};
|
julia = {JULIA_NUM_THREADS = "auto";};
|
||||||
|
|
@ -168,6 +165,8 @@
|
||||||
packageDefinitions =
|
packageDefinitions =
|
||||||
prev.packageDefinitions
|
prev.packageDefinitions
|
||||||
// {
|
// {
|
||||||
|
# Main package definition
|
||||||
|
# This creates the command with configured languages and tools
|
||||||
"${config.defaultPackageName}" = utils.mergeCatDefs prev.packageDefinitions.n (
|
"${config.defaultPackageName}" = utils.mergeCatDefs prev.packageDefinitions.n (
|
||||||
{
|
{
|
||||||
pkgs,
|
pkgs,
|
||||||
|
|
@ -178,8 +177,10 @@
|
||||||
suffix-path = false;
|
suffix-path = false;
|
||||||
suffix-LD = false;
|
suffix-LD = false;
|
||||||
aliases = ["pvim"];
|
aliases = ["pvim"];
|
||||||
|
# Import all host commands from hosts/ directory
|
||||||
hosts = import ./hosts config pkgs;
|
hosts = import ./hosts config pkgs;
|
||||||
};
|
};
|
||||||
|
# Enable/disable features based on config
|
||||||
categories = {
|
categories = {
|
||||||
julia = config.enabledLanguages.julia;
|
julia = config.enabledLanguages.julia;
|
||||||
python = config.enabledLanguages.python;
|
python = config.enabledLanguages.python;
|
||||||
|
|
@ -198,6 +199,7 @@
|
||||||
);
|
);
|
||||||
in {
|
in {
|
||||||
packages = projectConfig;
|
packages = projectConfig;
|
||||||
|
# Development shell configuration
|
||||||
devShells = forSystems (system: let
|
devShells = forSystems (system: let
|
||||||
pkgs = import nixpkgs {inherit system;};
|
pkgs = import nixpkgs {inherit system;};
|
||||||
in {
|
in {
|
||||||
|
|
@ -205,6 +207,7 @@
|
||||||
name = config.defaultPackageName;
|
name = config.defaultPackageName;
|
||||||
packages = [projectConfig.${system}.default];
|
packages = [projectConfig.${system}.default];
|
||||||
inputsFrom = [];
|
inputsFrom = [];
|
||||||
|
# Welcome message when entering the shell
|
||||||
shellHook = import ./lib/shell-hook.nix config pkgs;
|
shellHook = import ./lib/shell-hook.nix config pkgs;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
16
templates/rde/lib/mini-notify-config.lua
Normal file
16
templates/rde/lib/mini-notify-config.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
-- Configuration for mini.notify
|
||||||
|
-- Filters out some verbose LSP progress notifications
|
||||||
|
local predicate = function(notif)
|
||||||
|
if not (notif.data.source == "lsp_progress" and notif.data.client_name == "lua_ls") then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
-- Filter out some LSP progress notifications from 'lua_ls'
|
||||||
|
return notif.msg:find("Diagnosing") == nil and notif.msg:find("semantic tokens") == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local custom_sort = function(notif_arr)
|
||||||
|
return MiniNotify.default_sort(vim.tbl_filter(predicate, notif_arr))
|
||||||
|
end
|
||||||
|
|
||||||
|
require("mini.notify").setup({ content = { sort = custom_sort } })
|
||||||
|
vim.notify = MiniNotify.make_notify()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue