Skip to content

Commit 5897ad9

Browse files
authored
refactor(nix): restructure flake into modular components (#40)
* refactor(nix): restructure flake into modular components * Move package definitions and devShell into separate modules * Add new wrapper module for git-msg configuration * Reorganize treefmt configuration into its own module * Update source filtering in git-msg package definition * Implement new default.nix to auto-import all modules Signed-off-by: Qiming Chu <cchuqiming@gmail.com> * ci: enable pipe-operators experimental feature in Nix commands Signed-off-by: Qiming Chu <cchuqiming@gmail.com> --------- Signed-off-by: Qiming Chu <cchuqiming@gmail.com>
1 parent 2549864 commit 5897ad9

File tree

10 files changed

+209
-18
lines changed

10 files changed

+209
-18
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v4
1818
- uses: cachix/install-nix-action@v31
19+
with:
20+
extra_nix_config: |
21+
experimental-features = nix-command flakes pipe-operators
1922
- name: Flake check
2023
run: nix flake check --all-systems
2124
build:
@@ -27,6 +30,9 @@ jobs:
2730
steps:
2831
- uses: actions/checkout@v4
2932
- uses: cachix/install-nix-action@v31
33+
with:
34+
extra_nix_config: |
35+
experimental-features = nix-command flakes pipe-operators
3036
- name: Build Git Commit Generator
3137
run: nix build -L
3238
- name: Upload Git Commit Generator

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ jobs:
4343
fi
4444
echo "RELEASE_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
4545
- name: Setup Nix
46-
uses: DeterminateSystems/nix-installer-action@main
46+
uses: cachix/install-nix-action@v31
47+
with:
48+
extra_nix_config: |
49+
experimental-features = nix-command flakes pipe-operators
4750
- name: Setup project
4851
run: |
4952
nix build -L

.github/workflows/update.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ jobs:
1212
pull-requests: write
1313
steps:
1414
- uses: actions/checkout@v4
15-
- uses: DeterminateSystems/nix-installer-action@main
16-
- uses: DeterminateSystems/magic-nix-cache-action@main
15+
- uses: cachix/install-nix-action@v31
16+
with:
17+
extra_nix_config: |
18+
experimental-features = nix-command flakes pipe-operators
1719
- name: Update flake lock
1820
run: |
1921
nix flake update

flake.nix

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
];
2626
perSystem =
2727
{
28+
inputs',
2829
pkgs,
2930
system,
3031
...
@@ -40,19 +41,9 @@
4041
};
4142
in
4243
{
43-
packages = {
44-
git-msg = pkgs.callPackage ./nix/pkgs/git-msg.nix { };
45-
default = self.packages.${system}.git-msg;
46-
};
47-
devShells.default = pkgs.mkShell {
48-
inputsFrom = [ self.packages.${system}.git-msg ];
49-
buildInputs = with pkgs; [
50-
rust-analyzer
51-
rustfmt
52-
];
53-
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
54-
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
55-
};
44+
imports = [
45+
./nix
46+
];
5647
formatter = treefmtEval.config.build.wrapper;
5748
checks = {
5849
formatting = treefmtEval.config.build.check self;

nix/default.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
imports = builtins.readDir ./modules |> builtins.attrNames |> map (name: ./modules/${name});
3+
}

nix/modules/devShells/default.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{ pkgs, config, ... }:
2+
{
3+
devShells.default = pkgs.mkShell {
4+
inputsFrom = [ config.git-msg-bin ];
5+
buildInputs = with pkgs; [
6+
rust-analyzer
7+
rustfmt
8+
];
9+
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
10+
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
11+
};
12+
}

nix/modules/package/default.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
lib,
3+
config,
4+
pkgs,
5+
system,
6+
...
7+
}:
8+
let
9+
self = {
10+
packages = (lib.mapAttrs (name: pkg: config.mkGitMsgWrapper name pkg) config.packagesSet) // {
11+
git-msg = config.git-msg-bin;
12+
default = self.packages.git-msg;
13+
};
14+
};
15+
in
16+
self

nix/pkgs/git-msg.nix renamed to nix/modules/package/git-msg.nix

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ pkgs.pkgsStatic.rustPlatform.buildRustPackage {
1313
version = "0.2.0";
1414

1515
cargoLock = {
16-
lockFile = ./../../Cargo.lock;
16+
lockFile = ./../../../Cargo.lock;
1717
};
18-
src = ./../..;
18+
src =
19+
with lib.fileset;
20+
toSource {
21+
root = ./../../..;
22+
fileset = unions [
23+
./../../../src
24+
./../../../Cargo.lock
25+
./../../../Cargo.toml
26+
];
27+
};
1928

2029
PKG_CONFIG_PATH = "${pkgs.pkgsStatic.openssl.dev}/lib/pkgconfig";
2130

nix/modules/treefmt/default.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{ pkgs, ... }:
2+
{
3+
treefmt = {
4+
projectRootFile = "flake.nix";
5+
programs = {
6+
nixfmt.enable = true;
7+
nixfmt.package = pkgs.nixfmt-rfc-style;
8+
};
9+
flakeCheck = true;
10+
};
11+
}

nix/modules/wrapper/default.nix

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
pkgs,
3+
lib,
4+
config,
5+
inputs',
6+
...
7+
}:
8+
let
9+
message = lib.types.submodule {
10+
options = {
11+
style = lib.mkOption {
12+
type = lib.types.nullOr lib.types.str;
13+
default = "conventional";
14+
description = "The Git commit message format style";
15+
example = "conventional";
16+
};
17+
historyLength = lib.mkOption {
18+
type = lib.types.nullOr lib.types.number;
19+
default = null;
20+
description = "The length of the git history to be used for generating commit messages";
21+
example = 5;
22+
};
23+
modelType = lib.mkOption {
24+
type = lib.types.str;
25+
default = "deepseek-chat";
26+
description = "The LLM model type to be used for generating commit messages";
27+
example = "deepseek-chat"; # Or "deepseek-reasoner"
28+
};
29+
};
30+
};
31+
32+
pkgsType = lib.types.submodule {
33+
options = {
34+
format = lib.mkOption {
35+
type = message;
36+
default = { };
37+
description = "The format style of this pacakge";
38+
};
39+
40+
description = lib.mkOption {
41+
type = lib.types.str;
42+
default = "";
43+
description = "Description of this packages";
44+
};
45+
};
46+
};
47+
in
48+
{
49+
options = {
50+
git-msg-bin = lib.mkOption {
51+
type = lib.types.package;
52+
default = pkgs.callPackage ./../package/git-msg.nix { };
53+
description = "The Git commit message generator";
54+
};
55+
56+
packagesSet = lib.mkOption {
57+
type = lib.types.attrsOf pkgsType;
58+
default = { };
59+
description = "The available packages for this module";
60+
};
61+
62+
defaultPackage = lib.mkOption {
63+
type = lib.types.package;
64+
description = "The default package to be used for this module";
65+
};
66+
67+
mkGitMsgWrapper = lib.mkOption {
68+
type = lib.types.functionTo (lib.types.functionTo lib.types.package);
69+
description = "function to wrap different Git commit message generator in shell scripts";
70+
};
71+
};
72+
73+
config = {
74+
packagesSet = {
75+
conventional = {
76+
format = {
77+
style = "conventional";
78+
modelType = "deepseek-chat";
79+
historyLength = 5;
80+
};
81+
description = "Git commit message generator with conventional style";
82+
};
83+
84+
bracketed = {
85+
format = {
86+
style = "bracketed";
87+
modelType = "deepseek-chat";
88+
historyLength = 5;
89+
};
90+
description = "Git commit message generator with bracketed style";
91+
};
92+
93+
plain = {
94+
format = {
95+
style = "plain";
96+
modelType = "deepseek-chat";
97+
historyLength = 5;
98+
};
99+
description = "Git commit message generator with plain style";
100+
};
101+
};
102+
103+
mkGitMsgWrapper =
104+
name: pkg:
105+
let
106+
formatConfig = pkg.format;
107+
scriptsParams = [
108+
"--model=${formatConfig.modelType}"
109+
(lib.optionals (formatConfig.style != null) "--format=${formatConfig.style}")
110+
(lib.optionals (
111+
formatConfig.historyLength != null
112+
) "--git-history=${lib.toHexString formatConfig.historyLength}")
113+
];
114+
115+
in
116+
pkgs.writeShellApplication {
117+
name = "git-msg-${name}";
118+
runtimeInputs = with pkgs; [
119+
coreutils
120+
git
121+
config.git-msg-bin
122+
];
123+
text = ''
124+
echo "Welcome to Git Commit Generator Modules!"
125+
echo "Format: ${formatConfig.style}"
126+
echo "Git Commit Message Generator: ${config.git-msg-bin}"
127+
echo "Params: ${lib.concatStringsSep " " scriptsParams}"
128+
${config.git-msg-bin}/bin/git-msg ${lib.concatStringsSep " " scriptsParams}
129+
'';
130+
};
131+
132+
defaultPackage =
133+
let
134+
formatName = "conventional";
135+
in
136+
config.mkGitMsgWrapper formatName config.packagesSet.${formatName};
137+
};
138+
}

0 commit comments

Comments
 (0)