Skip to content

Commit 563b287

Browse files
authored
Merge pull request #67 from getchoo-contrib/dont-instantiate
Don't re-instantiate nixpkgs in flake and module
2 parents 773f614 + 412c3e4 commit 563b287

6 files changed

+107
-58
lines changed

docs/generated-module-options.md

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ boolean
1616

1717

1818

19+
## services\.comin\.package
20+
21+
22+
23+
The comin package to use\.
24+
25+
26+
27+
*Type:*
28+
null or package
29+
30+
31+
32+
*Default:*
33+
` "pkgs.comin or comin.packages.\${system}.default or null" `
34+
35+
36+
1937
## services\.comin\.debug
2038

2139
Whether to run comin in debug mode\. Be careful, secrets are shown!\.

flake.lock

+10-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+9-46
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,33 @@
11
{
22
description = "Comin - GitOps for NixOS Machines";
33

4+
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
5+
46
outputs = { self, nixpkgs }:
57
let
68
systems = [ "aarch64-linux" "x86_64-linux" ];
79
forAllSystems = nixpkgs.lib.genAttrs systems;
8-
nixpkgsFor = forAllSystems (system: import nixpkgs {
9-
inherit system;
10-
overlays = [ self.overlays.default ];
11-
});
10+
nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system});
1211
optionsDocFor = forAllSystems (system:
1312
import ./nix/module-options-doc.nix (nixpkgsFor."${system}")
1413
);
1514
in {
16-
overlays.default = final: prev: let
17-
# - safe.directory: this is to allow comin to fetch local repositories belonging
18-
# to other users. Otherwise, comin fails with:
19-
# Pull from remote 'local' failed: unknown error: fatal: detected dubious ownership in repository
20-
# - core.hooksPath: to avoid Git executing hooks from a repository belonging to another user
21-
gitConfigFile = final.writeTextFile {
22-
name = "git.config";
23-
text = ''
24-
[safe]
25-
directory = *
26-
[core]
27-
hooksPath = /dev/null
28-
'';
29-
};
30-
in {
31-
comin = final.buildGoModule rec {
32-
pname = "comin";
33-
version = "0.6.0";
34-
nativeCheckInputs = [ final.git ];
35-
src = final.lib.fileset.toSource {
36-
root = ./.;
37-
fileset = final.lib.fileset.unions [
38-
./cmd
39-
./internal
40-
./go.mod
41-
./go.sum
42-
./main.go
43-
];
44-
};
45-
vendorHash = "sha256-VP8y/iSBIXZFfSmhHsXkp6RxP+2DovX3PbEDtMUMyYE=";
46-
ldflags = [
47-
"-X github.com/nlewo/comin/cmd.version=${version}"
48-
];
49-
buildInputs = [ final.makeWrapper ];
50-
postInstall = ''
51-
# This is because Nix needs Git at runtime by the go-git library
52-
wrapProgram $out/bin/comin --set GIT_CONFIG_SYSTEM ${gitConfigFile} --prefix PATH : ${final.git}/bin
53-
'';
54-
};
15+
overlays.default = final: prev: {
16+
comin = final.callPackage ./nix/package.nix { };
5517
};
5618

5719
packages = forAllSystems (system: {
58-
default = nixpkgsFor."${system}".comin;
20+
comin = nixpkgsFor."${system}".callPackage ./nix/package.nix { };
21+
default = self.packages."${system}".comin;
5922
generate-module-options = optionsDocFor."${system}".optionsDocCommonMarkGenerator;
6023
});
6124
checks = forAllSystems (system: {
6225
module-options-doc = optionsDocFor."${system}".checkOptionsDocCommonMark;
6326
# I don't understand why nix flake check does't build packages.default
64-
package = nixpkgsFor."${system}".comin;
27+
package = self.packages."${system}".comin;
6528
});
6629

67-
nixosModules.comin = import ./nix/module.nix self.overlays.default;
30+
nixosModules.comin = nixpkgs.lib.modules.importApply ./nix/module.nix { inherit self; };
6831
devShells.x86_64-linux.default = let
6932
pkgs = nixpkgs.legacyPackages.x86_64-linux;
7033
in pkgs.mkShell {

nix/module-options.nix

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
Whether to run the comin service.
99
'';
1010
};
11+
package = lib.mkPackageOption pkgs "comin" { nullable = true; } // {
12+
defaultText = "pkgs.comin or comin.packages.\${system}.default or null";
13+
};
1114
hostname = mkOption {
1215
type = str;
1316
default = config.networking.hostName;
@@ -47,7 +50,7 @@
4750
openFirewall = mkOption {
4851
type = types.bool;
4952
default = false;
50-
description = lib.mdDoc ''
53+
description = ''
5154
Open port in firewall for incoming connections to the Prometheus exporter.
5255
'';
5356
};

nix/module.nix

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
overlay: { config, pkgs, lib, ... }:
1+
self: { config, pkgs, lib, ... }:
22
let
33
cfg = config;
44
yaml = pkgs.formats.yaml { };
@@ -13,12 +13,22 @@ let
1313
};
1414
};
1515
cominConfigYaml = yaml.generate "comin.yaml" cominConfig;
16+
17+
inherit (pkgs.stdenv.hostPlatform) system;
18+
inherit (cfg.services.comin) package;
1619
in {
1720
imports = [ ./module-options.nix ];
1821
config = lib.mkIf cfg.services.comin.enable {
19-
nixpkgs.overlays = [ overlay ];
20-
environment.systemPackages = [ pkgs.comin ];
22+
assertions = [
23+
{ assertion = package != null; message = "`services.comin.package` cannot be null."; }
24+
# If the package is null and our `system` isn't supported by the Flake, it's probably safe to show this error message
25+
{ assertion = package == null -> lib.elem system (lib.attrNames self.packages); message = "comin: ${system} is not supported by the Flake."; }
26+
];
27+
28+
environment.systemPackages = [ package ];
2129
networking.firewall.allowedTCPPorts = lib.optional cfg.services.comin.exporter.openFirewall cfg.services.comin.exporter.port;
30+
# Use package from overlay first, then Flake package if available
31+
services.comin.package = lib.mkDefault pkgs.comin or self.packages.${system}.comin or null;
2232
systemd.services.comin = {
2333
wantedBy = [ "multi-user.target" ];
2434
path = [ config.nix.package ];
@@ -27,7 +37,7 @@ in {
2737
restartIfChanged = false;
2838
serviceConfig = {
2939
ExecStart =
30-
"${pkgs.comin}/bin/comin "
40+
(lib.getExe package)
3141
+ (lib.optionalString cfg.services.comin.debug "--debug ")
3242
+ " run "
3343
+ "--config ${cominConfigYaml}";

nix/package.nix

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
lib,
3+
buildGoModule,
4+
git,
5+
makeWrapper,
6+
writeTextFile,
7+
}:
8+
9+
let
10+
# - safe.directory: this is to allow comin to fetch local repositories belonging
11+
# to other users. Otherwise, comin fails with:
12+
# Pull from remote 'local' failed: unknown error: fatal: detected dubious ownership in repository
13+
# - core.hooksPath: to avoid Git executing hooks from a repository belonging to another user
14+
gitConfigFile = writeTextFile {
15+
name = "git.config";
16+
text = ''
17+
[safe]
18+
directory = *
19+
[core]
20+
hooksPath = /dev/null
21+
'';
22+
};
23+
in
24+
25+
buildGoModule rec {
26+
pname = "comin";
27+
version = "0.6.0";
28+
nativeCheckInputs = [ git ];
29+
src = lib.fileset.toSource {
30+
root = ../.;
31+
fileset = lib.fileset.unions [
32+
../cmd
33+
../internal
34+
../go.mod
35+
../go.sum
36+
../main.go
37+
];
38+
};
39+
vendorHash = "sha256-VP8y/iSBIXZFfSmhHsXkp6RxP+2DovX3PbEDtMUMyYE=";
40+
ldflags = [
41+
"-X github.com/nlewo/comin/cmd.version=${version}"
42+
];
43+
buildInputs = [ makeWrapper ];
44+
postInstall = ''
45+
# This is because Nix needs Git at runtime by the go-git library
46+
wrapProgram $out/bin/comin --set GIT_CONFIG_SYSTEM ${gitConfigFile} --prefix PATH : ${git}/bin
47+
'';
48+
49+
meta = {
50+
mainProgram = "comin";
51+
};
52+
}

0 commit comments

Comments
 (0)