From 8ef7d74438445f4fadc8b7b0225b86e7daa0ac57 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 18 Nov 2024 17:24:33 +0800 Subject: [PATCH] treewide: init flakes --- .envrc | 1 + .gitignore | 3 + LICENSE | 21 ++++ flake.lock | 182 +++++++++++++++++++++++++++++++++ flake.nix | 76 ++++++++++++++ flake/modules/nixpkgs.nix | 86 ++++++++++++++++ flake/modules/passthru.nix | 9 ++ flake/outputs/devshell.nix | 20 ++++ flake/outputs/flake-module.nix | 9 ++ flake/outputs/treefmt.nix | 29 ++++++ justfile | 9 ++ lib/default.nix | 14 +++ lib/path.nix | 42 ++++++++ 13 files changed, 501 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 flake/modules/nixpkgs.nix create mode 100644 flake/modules/passthru.nix create mode 100644 flake/outputs/devshell.nix create mode 100644 flake/outputs/flake-module.nix create mode 100644 flake/outputs/treefmt.nix create mode 100644 justfile create mode 100644 lib/default.nix create mode 100644 lib/path.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c8d374 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +result* +.direnv +.pre-commit-config.yaml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b559963 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 rebmit + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ca27557 --- /dev/null +++ b/flake.lock @@ -0,0 +1,182 @@ +{ + "nodes": { + "devshell": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1728330715, + "narHash": "sha256-xRJ2nPOXb//u1jaBnDP56M7v5ldavjbtR6lfGqSvcKg=", + "owner": "numtide", + "repo": "devshell", + "rev": "dd6b80932022cea34a019e2bb32f6fa9e494dfef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1730504689, + "narHash": "sha256-hgmguH29K2fvs9szpq2r3pz2/8cJd2LPS+b4tfNFCwE=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "506278e768c2a08bec68eb62932193e341f55c90", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "git-hooks-nix": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "gitignore": [ + "gitignore-nix" + ], + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-stable": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1731363552, + "narHash": "sha256-vFta1uHnD29VUY4HJOO/D6p6rxyObnf+InnSMT4jlMU=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "cd1af27aa85026ac759d5d3fccf650abe7e1bbf0", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "haumea": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1708375098, + "narHash": "sha256-DaFJp3wDHgOqx98U0SF57bXaH2Orp106c+jSdPCVu1E=", + "owner": "nix-community", + "repo": "haumea", + "rev": "ec6350fd9353e7f27ce0e85d31f82e3ed73e4d70", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "haumea", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1731676054, + "narHash": "sha256-OZiZ3m8SCMfh3B6bfGC/Bm4x3qc1m2SVEAlkV6iY7Yg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5e4fbfb6b3de1aa2872b76d49fafc942626e2add", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devshell": "devshell", + "flake-compat": "flake-compat", + "flake-parts": "flake-parts", + "git-hooks-nix": "git-hooks-nix", + "gitignore-nix": "gitignore-nix", + "haumea": "haumea", + "nixpkgs": [ + "nixpkgs-unstable" + ], + "nixpkgs-unstable": "nixpkgs-unstable", + "treefmt-nix": "treefmt-nix" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1730321837, + "narHash": "sha256-vK+a09qq19QNu2MlLcvN4qcRctJbqWkX7ahgPZ/+maI=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "746901bb8dba96d154b66492a29f5db0693dbfcc", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b0045a1 --- /dev/null +++ b/flake.nix @@ -0,0 +1,76 @@ +{ + description = "a collection of common nix expressions used by rebmit"; + + outputs = + inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } ( + { config, lib, ... }: + let + selfLib = import ./lib { inherit inputs lib; }; + in + { + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + flake.lib = selfLib // { + inherit (config) systems; + }; + imports = [ + inputs.devshell.flakeModule + inputs.git-hooks-nix.flakeModule + inputs.treefmt-nix.flakeModule + ] ++ selfLib.path.buildModuleList ./flake; + } + ); + + inputs = { + # flake-parts + + flake-parts = { + url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs-lib.follows = "nixpkgs"; + }; + + # nixpkgs + + nixpkgs.follows = "nixpkgs-unstable"; + nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable"; + + # flake modules + + devshell = { + url = "github:numtide/devshell"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + git-hooks-nix = { + url = "github:cachix/git-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.nixpkgs-stable.follows = "nixpkgs"; + inputs.flake-compat.follows = "flake-compat"; + inputs.gitignore.follows = "gitignore-nix"; + }; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + # libraries + + gitignore-nix = { + url = "github:hercules-ci/gitignore.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + haumea = { + url = "github:nix-community/haumea"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + # misc + + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + }; +} diff --git a/flake/modules/nixpkgs.nix b/flake/modules/nixpkgs.nix new file mode 100644 index 0000000..31803c6 --- /dev/null +++ b/flake/modules/nixpkgs.nix @@ -0,0 +1,86 @@ +# Portions of this file are sourced from +# https://github.com/linyinfeng/nur-packages/blob/73fea6901c19df2f480e734a75bc22dbabde3a53/flake-modules/nixpkgs.nix +{ + inputs, + flake-parts-lib, + ... +}: +let + inherit (flake-parts-lib) mkPerSystemOption; +in +{ + options.perSystem = mkPerSystemOption ( + { + config, + system, + lib, + ... + }: + let + cfg = config.nixpkgs; + in + with lib; + { + _file = ./nixpkgs.nix; + # https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/default.nix + options.nixpkgs = { + path = mkOption { + type = types.path; + default = inputs.nixpkgs; + defaultText = "inputs.nixpkgs"; + description = '' + Path to nixpkgs to be imported. + ''; + }; + localSystem = mkOption { + type = types.attrs; + default = { + inherit system; + }; + description = '' + The system packages will be built on. + ''; + }; + crossSystem = mkOption { + type = with types; nullOr attrs; + default = null; + description = '' + The system packages will ultimately be run on. + ''; + }; + config = mkOption { + type = with types; attrsOf raw; + default = { }; + description = '' + Allow a configuration attribute set to be passed in as an argument. + ''; + }; + overlays = mkOption { + type = with types; listOf raw; + default = [ ]; + description = '' + List of overlays layers used to extend Nixpkgs. + ''; + }; + crossOverlays = mkOption { + type = with types; listOf raw; + default = [ ]; + description = '' + List of overlays to apply to target packages only. + ''; + }; + }; + config = { + _module.args.pkgs = import cfg.path { + inherit (cfg) + localSystem + crossSystem + config + overlays + crossOverlays + ; + }; + }; + } + ); +} diff --git a/flake/modules/passthru.nix b/flake/modules/passthru.nix new file mode 100644 index 0000000..e3250d3 --- /dev/null +++ b/flake/modules/passthru.nix @@ -0,0 +1,9 @@ +# Portions of this file are sourced from +# https://github.com/linyinfeng/nur-packages/blob/73fea6901c19df2f480e734a75bc22dbabde3a53/flake-modules/passthru.nix +{ lib, ... }: +{ + options.passthru = lib.mkOption { + visible = false; + type = with lib.types; attrsOf unspecified; + }; +} diff --git a/flake/outputs/devshell.nix b/flake/outputs/devshell.nix new file mode 100644 index 0000000..0979a6d --- /dev/null +++ b/flake/outputs/devshell.nix @@ -0,0 +1,20 @@ +{ + perSystem = + { + config, + pkgs, + lib, + ... + }: + { + devshells.default = { + packages = with pkgs; [ + just + ]; + env = [ + (lib.nameValuePair "DEVSHELL_NO_MOTD" 1) + ]; + devshell.startup.pre-commit-hook.text = config.pre-commit.installationScript; + }; + }; +} diff --git a/flake/outputs/flake-module.nix b/flake/outputs/flake-module.nix new file mode 100644 index 0000000..56808e1 --- /dev/null +++ b/flake/outputs/flake-module.nix @@ -0,0 +1,9 @@ +{ self, ... }: +let + inherit (self.lib.path) buildModuleList; +in +{ + flake.flakeModule = { + imports = buildModuleList ../modules; + }; +} diff --git a/flake/outputs/treefmt.nix b/flake/outputs/treefmt.nix new file mode 100644 index 0000000..41faef2 --- /dev/null +++ b/flake/outputs/treefmt.nix @@ -0,0 +1,29 @@ +{ + perSystem = + { + config, + lib, + ... + }: + { + treefmt = { + projectRootFile = "flake.nix"; + programs = { + nixfmt.enable = true; + deadnix.enable = true; + prettier.enable = true; + }; + }; + + devshells.default.packages = lib.singleton config.treefmt.build.wrapper; + + pre-commit.settings.hooks = { + treefmt = { + enable = true; + name = "treefmt"; + entry = lib.getExe config.treefmt.build.wrapper; + pass_filenames = false; + }; + }; + }; +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..fe742b1 --- /dev/null +++ b/justfile @@ -0,0 +1,9 @@ +set shell := ["bash", "-c"] + +[group('nix')] +up: + nix flake update + +[group('nix')] +upp input: + nix flake lock --update-input {{input}} diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..de170d5 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,14 @@ +{ inputs, lib }: +let + inherit (lib) makeExtensible; + self = makeExtensible ( + self: + let + callLibs = file: import file { inherit inputs lib self; }; + in + { + path = callLibs ./path.nix; + } + ); +in +self diff --git a/lib/path.nix b/lib/path.nix new file mode 100644 index 0000000..d452a77 --- /dev/null +++ b/lib/path.nix @@ -0,0 +1,42 @@ +# Portions of this file are sourced from +# https://github.com/linyinfeng/dotfiles/tree/b618b0fd16fb9c79ab7199ed51c4c0f98a392cea/lib +{ + inputs, + lib, + ... +}: +let + haumea = inputs.haumea.lib; + + rakeLeaves = + src: + haumea.load { + inherit src; + loader = haumea.loaders.path; + transformer = + _cursor: dir: + if dir ? default then + assert (lib.attrNames dir == [ "default" ]); + dir.default + else + dir; + }; + + flattenTree = + tree: + let + mkNewPrefix = prefix: name: "${if prefix == "" then "" else "${prefix}/"}${name}"; + flattenTree' = + prefix: remain: + if lib.isAttrs remain then + lib.flatten (lib.mapAttrsToList (name: value: flattenTree' (mkNewPrefix prefix name) value) remain) + else + [ (lib.nameValuePair prefix remain) ]; + in + lib.listToAttrs (flattenTree' "" tree); + + buildModuleList = dir: lib.attrValues (flattenTree (rakeLeaves dir)); +in +{ + inherit rakeLeaves flattenTree buildModuleList; +}