chore: set up treefmt-nix and reformat
19 files changed, 256 insertions(+), 126 deletions(-)
changed files
- default.nix
- docs/developing.md
- docs/running.md
- flake.lock
- flake.nix
- internal/config/config.go
- internal/config/default.go
- internal/importer/main.go
- internal/importer/options.go
- internal/importer/package.go
- internal/index/nixattr/nixattr.go
- internal/index/nixattr/nixattr_test.go
- internal/server/mux.go
- nix/dev-shell.nix
- nix/modules/default.nix
- nix/modules/source-options.nix
- nix/overlays/bleve.nix
- nix/package.nix
- nix/pre-commit-checks.nix
M default.nix → default.nix
@@ -1,3 +1,5 @@ -{ system ? builtins.currentSystem }: +{ + system ? builtins.currentSystem, +}: (builtins.getFlake (toString ./.)).packages.${system}.default
M docs/developing.md → docs/developing.md
@@ -2,11 +2,11 @@ # Development ### Pre-requisites -- [Nix (the package manager)](https://nixos.org/download/) with [flakes enabled](https://nixos.wiki/wiki/flakes#Enable_flakes_temporarily) -- Optional: - - [cachix](https://docs.cachix.org/installation#installation) - - [lorri](https://github.com/nix-community/lorri) - - [direnv](https://direnv.net/) +- [Nix (the package manager)](https://nixos.org/download/) with [flakes enabled](https://nixos.wiki/wiki/flakes#Enable_flakes_temporarily) +- Optional: + - [cachix](https://docs.cachix.org/installation#installation) + - [lorri](https://github.com/nix-community/lorri) + - [direnv](https://direnv.net/) ### Setup@@ -16,13 +16,13 @@ ```shell-session cachix use pre-commit-hooks ``` -2. Start a nix shell (this will take some time) +1. Start a nix shell (this will take some time) ```shell-session nix develop ``` -3. Optional: install direnv, integrate it with your shell and then run +1. Optional: install direnv, integrate it with your shell and then run ```shell-session ln -s .envrc.sample .envrc
M docs/running.md → docs/running.md
@@ -2,8 +2,8 @@ # Running an instance ## Requirements: -- 400MiB disk space -- ~100MiB RAM (requires setting `Importer.LowMemory = true`) +- 400MiB disk space +- ~100MiB RAM (requires setting `Importer.LowMemory = true`) This [repository](https://git.alanpearce.eu/searchix) (or its [codeberg mirror](https://codeberg.org/alanpearce/searchix)) is importable as a flake containing a NixOS module.
M flake.lock → flake.lock
@@ -94,6 +94,22 @@ "repo": "nixpkgs", "type": "github" } }, + "nixpkgs_2": { + "locked": { + "lastModified": 1747958103, + "narHash": "sha256-qmmFCrfBwSHoWw7cVK4Aj+fns+c54EBP8cGqp/yK410=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "fe51d34885f7b5e3e7b59572796e1bcb427eccb1", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "pre-commit-hooks": { "inputs": { "flake-compat": "flake-compat",@@ -122,7 +138,8 @@ "flake-utils": "flake-utils", "gomod2nix": "gomod2nix", "nixpkgs": "nixpkgs", "pre-commit-hooks": "pre-commit-hooks", - "simple-css": "simple-css" + "simple-css": "simple-css", + "treefmt-nix": "treefmt-nix" } }, "simple-css": {@@ -149,6 +166,24 @@ }, "original": { "owner": "nix-systems", "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1750931469, + "narHash": "sha256-0IEdQB1nS+uViQw4k3VGUXntjkDp7aAlqcxdewb/hAc=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "ac8e6f32e11e9c7f153823abc3ab007f2a65d3e1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", "type": "github" } }
M flake.nix → flake.nix
@@ -15,52 +15,81 @@ nixpkgs.follows = "nixpkgs"; flake-utils.follows = "flake-utils"; }; }; + treefmt-nix.url = "github:numtide/treefmt-nix"; simple-css = { url = "https://raw.githubusercontent.com/kevquirk/simple.css/v2.3.4/simple.css"; flake = false; }; }; - outputs = { self, nixpkgs, flake-utils, gomod2nix, pre-commit-hooks, simple-css }: + outputs = + { + self, + nixpkgs, + flake-utils, + gomod2nix, + pre-commit-hooks, + treefmt-nix, + simple-css, + }: { nixosModules = { web = import ./nix/modules self; }; - } // (flake-utils.lib.eachDefaultSystem - (system: - let - pkgs = import nixpkgs { - inherit system; - overlays = [ - (import ./nix/overlays) - gomod2nix.overlays.default - ]; + } + // (flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + (import ./nix/overlays) + gomod2nix.overlays.default + ]; + }; + + treefmtEval = treefmt-nix.lib.evalModule pkgs { + projectRootFile = "flake.nix"; + programs = { + nixfmt.enable = true; + taplo.enable = true; + prettier.enable = true; + gofumpt.enable = true; + golines.enable = true; + sqlfluff.enable = true; + sqlfluff.dialect = "sqlite"; }; - in - rec { - packages.css = "${simple-css}"; - packages.default = pkgs.callPackage ./nix/package.nix { - inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; - css = simple-css; + settings.formatter = { + taplo.excludes = [ "gomod2nix.toml" ]; }; - devShells.default = pkgs.callPackage ./nix/dev-shell.nix { - pre-commit-check = { - inherit (self.checks.${system}.pre-commit-check) - shellHook - enabledPackages; - }; - inherit (gomod2nix.legacyPackages.${system}) mkGoEnv gomod2nix; + }; + in + rec { + packages.css = "${simple-css}"; + packages.default = pkgs.callPackage ./nix/package.nix { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; + css = simple-css; + }; + formatter = treefmtEval.config.build.wrapper; + devShells.default = pkgs.callPackage ./nix/dev-shell.nix { + pre-commit-check = { + inherit (self.checks.${system}.pre-commit-check) + shellHook + enabledPackages + ; }; - checks = { - pre-commit-check = pre-commit-hooks.lib.${system}.run ( - import ./nix/pre-commit-checks.nix { - inherit pkgs; - } - ); - buildVersion = pkgs.testers.testVersion { - package = packages.default; - }; + inherit (gomod2nix.legacyPackages.${system}) mkGoEnv gomod2nix; + }; + checks = { + pre-commit-check = pre-commit-hooks.lib.${system}.run ( + import ./nix/pre-commit-checks.nix { + inherit pkgs treefmtEval; + } + ); + buildVersion = pkgs.testers.testVersion { + package = packages.default; }; - }) - ); + }; + } + )); }
M internal/config/config.go → internal/config/config.go
@@ -11,8 +11,10 @@ "github.com/pelletier/go-toml/v2" "gitlab.com/tozd/go/errors" ) -var Version string -var DevMode bool +var ( + Version string + DevMode bool +) const MaxResultsShowAll = 10_000@@ -26,7 +28,6 @@ } func (u *URL) UnmarshalText(text []byte) (err error) { u.URL, err = url.Parse(string(text)) - if err != nil { return errors.WithMessagef(err, "could not parse URL %s", string(text)) }
M internal/config/default.go → internal/config/default.go
@@ -13,8 +13,10 @@ Owner: "NixOS", Repo: "nixpkgs", } -const none = "'none'" -const self = "'self'" +const ( + none = "'none'" + self = "'self'" +) const maxAge = (1 * 365 * 24 * time.Hour)
M internal/importer/main.go → internal/importer/main.go
@@ -76,7 +76,6 @@ previousUpdate := sourceMeta.Updated ctx, cancel := context.WithTimeout(parent, source.Timeout.Duration) defer cancel() files, err := fetcher.FetchIfNeeded(ctx, sourceMeta) - if err != nil { var exerr *exec.ExitError if errors.As(err, &exerr) {@@ -233,7 +232,6 @@ func New( cfg *config.Config, options *Options, ) (*Importer, errors.E) { - return &Importer{ config: cfg, log: options.Logger,
M internal/importer/options.go → internal/importer/options.go
@@ -125,7 +125,6 @@ x := kv.Value.(map[string]interface{}) var decls []*nix.Link for _, decl := range x["declarations"].([]interface{}) { - switch decl := reflect.ValueOf(decl); decl.Kind() { case reflect.String: s := decl.String()@@ -164,7 +163,7 @@ continue } - var decs = make([]nix.Link, len(i.optJSON.Declarations)) + decs := make([]nix.Link, len(i.optJSON.Declarations)) for i, d := range i.optJSON.Declarations { decs[i] = nix.Link(d) }
M internal/importer/package.go → internal/importer/package.go
@@ -196,7 +196,7 @@ delete(meta, "license") } if meta["platforms"] != nil { - var plats = make([]any, len(meta["platforms"].([]any))) + plats := make([]any, len(meta["platforms"].([]any))) i := 0 for _, plat := range meta["platforms"].([]interface{}) { switch v := reflect.ValueOf(plat); v.Kind() {@@ -239,7 +239,7 @@ var maints []nix.Maintainer if meta["maintainers"] != nil { switch maint := reflect.ValueOf(meta["maintainers"]); maint.Kind() { case reflect.String: - maints = []nix.Maintainer{nix.Maintainer{Name: maint.String(), Github: maint.String()}} + maints = []nix.Maintainer{{Name: maint.String(), Github: maint.String()}} case reflect.Slice, reflect.Array: maints = make([]nix.Maintainer, maint.Len()) for i, val := range maint.Slice(0, maint.Len()).Interface().([]any) {
M internal/index/nixattr/nixattr.go → internal/index/nixattr/nixattr.go
@@ -78,5 +78,7 @@ return NewFilter(), nil } func init() { - registry.RegisterTokenFilter(Name, FilterConstructor) + if err := registry.RegisterTokenFilter(Name, FilterConstructor); err != nil { + panic(err) + } }
M internal/index/nixattr/nixattr_test.go → internal/index/nixattr/nixattr_test.go
@@ -22,7 +22,6 @@ "github.com/blevesearch/bleve/v2/analysis" ) func TestNixAttrFilter(t *testing.T) { - tests := []struct { input analysis.TokenStream output analysis.TokenStream
M internal/server/mux.go → internal/server/mux.go
@@ -34,9 +34,7 @@ Message string Code int } -var ( - sources []*config.Source -) +var sources []*config.Source func applyDevModeOverrides(cfg *config.Config) { if len(cfg.Web.ContentSecurityPolicy.ScriptSrc) == 0 {@@ -103,8 +101,8 @@ return } - var pageSize = search.DefaultPageSize - var pageNumber = 1 + pageSize := search.DefaultPageSize + pageNumber := 1 if pg := r.URL.Query().Get("page"); pg != "" { pageNumber, err = strconv.Atoi(pg) if err != nil || pageNumber > math.MaxInt {
M nix/dev-shell.nix → nix/dev-shell.nix
@@ -1,23 +1,27 @@ -{ pre-commit-check -, mkGoEnv -, gomod2nix -, mkShell -, pkgs +{ + pre-commit-check, + mkGoEnv, + gomod2nix, + mkShell, + pkgs, }: let goEnv = mkGoEnv { pwd = ../.; }; in mkShell { - packages = with pkgs; [ - goEnv + packages = + with pkgs; + [ + goEnv - sd - modd - brotli - bleve - wgo - gomod2nix - nixpkgs-lint - ] ++ pre-commit-check.enabledPackages; + sd + modd + brotli + bleve + wgo + gomod2nix + nixpkgs-lint + ] + ++ pre-commit-check.enabledPackages; inherit (pre-commit-check) shellHook; }
M nix/modules/default.nix → nix/modules/default.nix
@@ -1,9 +1,10 @@ self: -{ config -, lib -, pkgs -, ... +{ + config, + lib, + pkgs, + ... }: let@@ -41,16 +42,29 @@ ProtectControlGroups = true; ProtectKernelLogs = true; ProtectKernelModules = true; ProtectKernelTunables = true; - RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ]; + RestrictAddressFamilies = [ + "AF_UNIX" + "AF_INET" + "AF_INET6" + ]; RestrictNamespaces = true; RestrictRealtime = true; RestrictSUIDSGID = true; SystemCallArchitectures = "native"; - SystemCallFilter = [ "@system-service" "~@privileged @setuid @keyring" ]; + SystemCallFilter = [ + "@system-service" + "~@privileged @setuid @keyring" + ]; UMask = "0066"; }; - inherit (lib) mkEnableOption mkOption mkIf optionalAttrs types; + inherit (lib) + mkEnableOption + mkOption + mkIf + optionalAttrs + types + ; in { options.services.searchix = {@@ -75,7 +89,15 @@ description = "Home directory for searchix user"; }; environment = mkOption { - type = with types; attrsOf (nullOr (oneOf [ str path package ])); + type = + with types; + attrsOf ( + nullOr (oneOf [ + str + path + package + ]) + ); default = { }; description = "Environment variables passed to the service process."; };@@ -92,7 +114,14 @@ default = "${cfg.homeDir}/data"; }; logLevel = mkOption { - type = with types; enum [ "error" "warn" "info" "debug" ]; + type = + with types; + enum [ + "error" + "warn" + "info" + "debug" + ]; description = "Only log messages with the given severity or above."; default = "info"; };@@ -182,10 +211,15 @@ description = "Time of day to fetch and import new options."; }; sources = mkOption { - type = with types; - attrsOf (submodule (import ./source-options.nix { - inherit cfg settingsFormat; - })); + type = + with types; + attrsOf ( + submodule ( + import ./source-options.nix { + inherit cfg settingsFormat; + } + ) + ); default = { nixos.enable = true; nixpkgs.enable = true;@@ -211,12 +245,15 @@ description = "Searchix Nix option search"; wantedBy = [ "multi-user.target" ]; path = with pkgs; [ nix ]; inherit (cfg) environment; - serviceConfig = defaultServiceConfig // { - ExecStart = "${package}/bin/searchix-web --config ${(settingsFormat.generate "searchix-config.toml" cfg.settings)}"; - } // lib.optionalAttrs (cfg.settings.web.port < 1024) { - AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; - CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ]; - }; + serviceConfig = + defaultServiceConfig + // { + ExecStart = "${package}/bin/searchix-web --config ${(settingsFormat.generate "searchix-config.toml" cfg.settings)}"; + } + // lib.optionalAttrs (cfg.settings.web.port < 1024) { + AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; + CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ]; + }; }; users.users = optionalAttrs (cfg.user == "searchix") {
M nix/modules/source-options.nix → nix/modules/source-options.nix
@@ -1,7 +1,15 @@ -{ cfg, settingsFormat }: -{ config, lib, name, ... }: +{ settingsFormat, ... }: +{ + lib, + name, + ... +}: let - inherit (lib) literalExpression mkOption mkEnableOption types; + inherit (lib) + mkOption + mkEnableOption + types + ; in { freeformType = settingsFormat.type;
M nix/overlays/bleve.nix → nix/overlays/bleve.nix
@@ -1,8 +1,9 @@ -{ lib -, stdenv -, fetchFromGitHub -, installShellFiles -, buildGoModule +{ + lib, + stdenv, + fetchFromGitHub, + installShellFiles, + buildGoModule, }: let gomod = builtins.fromTOML (builtins.readFile ./../../gomod2nix.toml);
M nix/package.nix → nix/package.nix
@@ -1,4 +1,5 @@ -{ pkgs ? ( +{ + pkgs ? ( let inherit (builtins) fetchTree fromJSON readFile; inherit ((fromJSON (readFile ../flake.lock)).nodes) nixpkgs gomod2nix;@@ -8,9 +9,9 @@ overlays = [ (import "${fetchTree gomod2nix.locked}/overlay.nix") ]; } - ) -, buildGoApplication ? pkgs.buildGoApplication -, css + ), + buildGoApplication ? pkgs.buildGoApplication, + css, }: let version = "0.1.44";@@ -18,19 +19,19 @@ in buildGoApplication { pname = "searchix"; inherit version; - src = with pkgs.lib.fileset; toSource { - root = ../.; - fileset = difference - (unions [ + src = + with pkgs.lib.fileset; + toSource { + root = ../.; + fileset = difference (unions [ ../go.mod ../go.sum ../internal ../frontend ../cmd ../web - ]) - (maybeMissing ../frontend/static/base.css); - }; + ]) (maybeMissing ../frontend/static/base.css); + }; subPackages = [ "cmd/searchix-web" ];
M nix/pre-commit-checks.nix → nix/pre-commit-checks.nix
@@ -1,5 +1,5 @@ -{ pkgs }: -rec { +{ pkgs, treefmtEval }: +{ src = ../.; hooks = { gotest.enable = false;@@ -18,10 +18,6 @@ enable = true; # excludes is a regex excludes = [ "nix/overlays/.*" ]; }; - nixpkgs-fmt = { - enable = true; - excludes = [ "nix/overlays/.*" ]; - }; convco.enable = true;@@ -29,11 +25,18 @@ check-case-conflicts.enable = true; check-symlinks.enable = true; editorconfig-checker = { enable = true; - excludes = [ "\.md$" "\.ya?ml$" ]; + excludes = [ + "\.md$" + "\.ya?ml$" + ]; }; prettier = { enable = true; - types_or = [ "plain-text" "yaml" "javascript" ]; + types_or = [ + "plain-text" + "yaml" + "javascript" + ]; excludes = [ "CHANGELOG\.md$" ]; };@@ -52,7 +55,10 @@ go-mod-tidy = { enable = true; name = "go-mod-tidy"; description = "Run `go mod tidy`"; - types_or = [ "go" "go-mod" ]; + types_or = [ + "go" + "go-mod" + ]; entry = "${pkgs.go}/bin/go mod tidy"; pass_filenames = false; };@@ -70,7 +76,10 @@ enable = true; name = "generate-default-config"; description = "Ensure default configuration file is up-to-date"; files = "^defaults.toml|internal/config/"; - types_or = [ "go" "toml" ]; + types_or = [ + "go" + "toml" + ]; entry = let script = pkgs.writeShellScript "generate-default-config" ''@@ -78,6 +87,11 @@ ${pkgs.wgo}/bin/wgo run -exit ./cmd/searchix-web --print-default-config > defaults.toml ''; in builtins.toString script; + }; + + treefmt = { + enable = true; + packageOverrides.treefmt = treefmtEval.config.build.wrapper; }; }; }