Files
grapho/modules/nb.nix
Brandon Lucas b40ac99524 Initial commit: Ultimate Notetaking, Sync & Backup System
A NixOS-based system for managing personal data across three tiers:
- Tier 1: Configuration (shareable via git)
- Tier 2: Syncable data (nb + Syncthing)
- Tier 3: Large data (self-hosted services + backup)

Includes:
- NixOS modules for nb, Syncthing, backup (restic)
- Server modules for Forgejo, Immich, Jellyfin
- Helper scripts (usync, ustatus)
- Comprehensive documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 01:44:00 -05:00

147 lines
4.1 KiB
Nix

# nb - CLI Notebook Module
#
# This module installs and configures nb, a command-line notebook tool
# with git-backed versioning and sync.
#
# Usage in your configuration.nix:
# programs.nb.enable = true;
# programs.nb.notebooks.personal.remote = "git@forgejo:you/notes.git";
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.nb;
in {
options.programs.nb = {
enable = mkEnableOption "nb notebook CLI";
package = mkOption {
type = types.package;
default = pkgs.nb;
defaultText = literalExpression "pkgs.nb";
description = "The nb package to use.";
};
defaultNotebook = mkOption {
type = types.str;
default = "home";
description = "The default notebook name.";
};
editor = mkOption {
type = types.str;
default = "nvim";
description = "Editor to use for editing notes.";
};
defaultExtension = mkOption {
type = types.str;
default = "md";
description = "Default file extension for new notes.";
};
autoSync = mkOption {
type = types.bool;
default = false;
description = ''
Whether to automatically sync notebooks after operations.
Note: This can slow down operations if network is slow.
'';
};
colorTheme = mkOption {
type = types.str;
default = "blacklight";
description = "Color theme for nb (see 'nb settings colors').";
};
notebooks = mkOption {
type = types.attrsOf (types.submodule {
options = {
remote = mkOption {
type = types.nullOr types.str;
default = null;
description = "Git remote URL for syncing this notebook.";
example = "git@github.com:user/notes.git";
};
encrypted = mkOption {
type = types.bool;
default = false;
description = "Whether to encrypt notes in this notebook.";
};
};
});
default = {};
description = "Notebooks to configure with their remotes.";
example = literalExpression ''
{
personal = { remote = "git@forgejo:user/personal.git"; };
work = { remote = "git@forgejo:user/work.git"; encrypted = true; };
}
'';
};
};
config = mkIf cfg.enable {
environment.systemPackages = [
cfg.package
pkgs.git # nb requires git
pkgs.bat # Optional: better file viewing
pkgs.w3m # Optional: for nb browse
];
# Set environment variables for nb
environment.variables = {
EDITOR = cfg.editor;
NB_DEFAULT_EXTENSION = cfg.defaultExtension;
NB_AUTO_SYNC = if cfg.autoSync then "1" else "0";
NB_COLOR_THEME = cfg.colorTheme;
};
# Create activation script to set up notebooks
system.activationScripts.nb-setup = let
notebookSetup = concatStringsSep "\n" (mapAttrsToList (name: opts: ''
# Create notebook if it doesn't exist
if ! ${cfg.package}/bin/nb notebooks | grep -q "^${name}$"; then
echo "Creating nb notebook: ${name}"
${cfg.package}/bin/nb notebooks add ${name}
fi
${optionalString (opts.remote != null) ''
# Set remote for notebook
echo "Setting remote for ${name}: ${opts.remote}"
${cfg.package}/bin/nb ${name}:remote set ${opts.remote} 2>/dev/null || true
''}
'') cfg.notebooks);
in stringAfter [ "users" ] ''
# Skip if running in a chroot or during initial install
if [ -d /home ]; then
${notebookSetup}
fi
'';
# Optional: systemd timer for periodic sync
# Uncomment if you want automatic background sync
#
# systemd.user.services.nb-sync = {
# description = "Sync nb notebooks";
# serviceConfig = {
# Type = "oneshot";
# ExecStart = "${cfg.package}/bin/nb sync --all";
# };
# };
#
# systemd.user.timers.nb-sync = {
# description = "Periodic nb sync";
# wantedBy = [ "timers.target" ];
# timerConfig = {
# OnCalendar = "*:0/15"; # Every 15 minutes
# Persistent = true;
# };
# };
};
}