From cf42f710c1930163ffb404bd6f489d31705801ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Sep 2023 07:43:01 +0200 Subject: [PATCH 1/5] expose flake interface that does not rely on overlays As creator of small tools, my flakes might often end up in other peoples projects. Overlays are a performance problem in those use cases because I would kept re-instantiating nixpkgs instances. Of course I could also expose my stuff as overlays as well. In that case I would need to merge downstream dependencies like go2modnix as well, which would polute their nixpkgs instance with unrelated stuff and also make it harder for my users to tell what random attributes came from which place. --- flake.nix | 28 +++++++++++++++++++++------- shell.nix | 6 ++++-- templates/app/default.nix | 3 ++- templates/app/flake.nix | 14 +++++++------- templates/app/shell.nix | 6 ++++-- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/flake.nix b/flake.nix index 85ee185..aa052e5 100644 --- a/flake.nix +++ b/flake.nix @@ -21,16 +21,30 @@ (utils.lib.eachDefaultSystem (system: let - pkgs = import nixpkgs { - inherit system; - overlays = [ - self.overlays.default - ]; + pkgs = nixpkgs.legacyPackages.${system}; + + # The newer Darwin SDK does not exist in current (nixos-22.05) stable + # branches, so just fallback to the default callPackage. + callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage; + + inherit (callPackage ./builder { + inherit gomod2nix; + }) mkGoEnv buildGoApplication; + gomod2nix = callPackage ./default.nix { + inherit mkGoEnv buildGoApplication; }; in { - packages.default = pkgs.callPackage ./. { }; - devShells.default = import ./shell.nix { inherit pkgs; }; + packages.default = gomod2nix; + legacyPackages = { + # we cannot put them in packages because they are builder functions + inherit mkGoEnv buildGoApplication; + # just have this here for convenience + inherit gomod2nix; + }; + devShells.default = callPackage ./shell.nix { + inherit mkGoEnv gomod2nix; + }; }) ); } diff --git a/shell.nix b/shell.nix index f80d988..f6229c6 100644 --- a/shell.nix +++ b/shell.nix @@ -8,6 +8,8 @@ ]; } ) +, gomod2nix ? pkgs.gomod2nix +, mkGoEnv ? pkgs.mkGoEnv }: pkgs.mkShell { @@ -15,7 +17,7 @@ pkgs.mkShell { nativeBuildInputs = [ pkgs.nixpkgs-fmt pkgs.golangci-lint - pkgs.gomod2nix - (pkgs.mkGoEnv { pwd = ./.; }) + gomod2nix + (mkGoEnv { pwd = ./.; }) ]; } diff --git a/templates/app/default.nix b/templates/app/default.nix index 2c29bbc..14ec4fe 100644 --- a/templates/app/default.nix +++ b/templates/app/default.nix @@ -9,9 +9,10 @@ ]; } ) +, buildGoApplication ? pkgs.buildGoApplication }: -pkgs.buildGoApplication { +buildGoApplication { pname = "myapp"; version = "0.1"; pwd = ./.; diff --git a/templates/app/flake.nix b/templates/app/flake.nix index 824ce7d..1238c4e 100644 --- a/templates/app/flake.nix +++ b/templates/app/flake.nix @@ -9,15 +9,15 @@ (flake-utils.lib.eachDefaultSystem (system: let - pkgs = import nixpkgs { - inherit system; - overlays = [ gomod2nix.overlays.default ]; - }; - + pkgs = nixpkgs.legacyPackages.${system}; in { - packages.default = pkgs.callPackage ./. { }; - devShells.default = import ./shell.nix { inherit pkgs; }; + packages.default = pkgs.callPackage ./. { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; + }; + devShells.default = pkgs.callPackage ./shell.nix { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication mkGoEnv gomod2nix; + }; }) ); } diff --git a/templates/app/shell.nix b/templates/app/shell.nix index b8e49a9..d4f21a8 100644 --- a/templates/app/shell.nix +++ b/templates/app/shell.nix @@ -9,14 +9,16 @@ ]; } ) +, mkGoEnv ? pkgs.mkGoEnv +, gomod2nix ? pkgs.gomod2nix }: let - goEnv = pkgs.mkGoEnv { pwd = ./.; }; + goEnv = mkGoEnv { pwd = ./.; }; in pkgs.mkShell { packages = [ goEnv - pkgs.gomod2nix + gomod2nix ]; } From c3d7cbf19a253411836b02241406d51144fabc56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Sep 2023 07:49:35 +0200 Subject: [PATCH 2/5] docs: update flake url --- docs/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index 5427b30..620da2c 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -63,7 +63,7 @@ pkgs.buildGoApplication { The quickest way to get started if using Nix Flakes is to use the Flake template: ``` bash -$ nix flake init -t github:tweag/gomod2nix#app +$ nix flake init -t github:nix-community/gomod2nix#app ``` ## Basic usage From f6cddc0ed865bf4352ae7e49a4ce1aa29df2ee2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Sep 2023 08:07:32 +0200 Subject: [PATCH 3/5] update the reason why we have to use a newer sdk for macOS --- flake.nix | 4 ++-- overlay.nix | 4 ++-- templates/app/flake.nix | 10 +++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index aa052e5..e80aef0 100644 --- a/flake.nix +++ b/flake.nix @@ -23,8 +23,8 @@ let pkgs = nixpkgs.legacyPackages.${system}; - # The newer Darwin SDK does not exist in current (nixos-22.05) stable - # branches, so just fallback to the default callPackage. + # The current default sdk for macOS fails to compile go projects, so we use a newer one for now. + # This has no effect on other platforms. callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage; inherit (callPackage ./builder { diff --git a/overlay.nix b/overlay.nix index 9010386..5154028 100644 --- a/overlay.nix +++ b/overlay.nix @@ -1,7 +1,7 @@ final: prev: let - # The newer Darwin SDK does not exist in current (nixos-22.05) stable - # branches, so just fallback to the default callPackage. + # The current default sdk for macOS fails to compile go projects, so we use a newer one for now. + # This has no effect on other platforms. callPackage = final.darwin.apple_sdk_11_0.callPackage or final.callPackage; in { diff --git a/templates/app/flake.nix b/templates/app/flake.nix index 1238c4e..8d12bbc 100644 --- a/templates/app/flake.nix +++ b/templates/app/flake.nix @@ -10,12 +10,16 @@ (system: let pkgs = nixpkgs.legacyPackages.${system}; + + # The current default sdk for macOS fails to compile go projects, so we use a newer one for now. + # This has no effect on other platforms. + callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage; in { - packages.default = pkgs.callPackage ./. { - inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; + packages.default = callPackage ./. { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; }; - devShells.default = pkgs.callPackage ./shell.nix { + devShells.default = callPackage ./shell.nix { inherit (gomod2nix.legacyPackages.${system}) buildGoApplication mkGoEnv gomod2nix; }; }) From 495a010f5e4358103cc24e74070aeea9294562bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Sep 2023 08:36:14 +0200 Subject: [PATCH 4/5] use flake-utils input and also override it in the template --- flake.lock | 38 ++++++++++++++++++++++++++++---------- flake.nix | 7 +++---- templates/app/flake.nix | 2 ++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/flake.lock b/flake.lock index 1340215..a0a6418 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,23 @@ { "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1658285632, @@ -18,22 +36,22 @@ }, "root": { "inputs": { - "nixpkgs": "nixpkgs", - "utils": "utils" + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" } }, - "utils": { + "systems": { "locked": { - "lastModified": 1653893745, - "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", + "owner": "nix-systems", + "repo": "default", "type": "github" } } diff --git a/flake.nix b/flake.nix index e80aef0..265dd7d 100644 --- a/flake.nix +++ b/flake.nix @@ -2,10 +2,9 @@ description = "Convert go.mod/go.sum to Nix packages"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/master"; + inputs.flake-utils.url = "github:numtide/flake-utils"; - inputs.utils.url = "github:numtide/flake-utils"; - - outputs = { self, nixpkgs, utils }: + outputs = { self, nixpkgs, flake-utils }: { overlays.default = import ./overlay.nix; @@ -18,7 +17,7 @@ defaultTemplate = self.templates.app; } // - (utils.lib.eachDefaultSystem + (flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; diff --git a/templates/app/flake.nix b/templates/app/flake.nix index 8d12bbc..b26947b 100644 --- a/templates/app/flake.nix +++ b/templates/app/flake.nix @@ -4,6 +4,8 @@ inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.gomod2nix.url = "github:nix-community/gomod2nix"; + inputs.gomod2nix.inputs.nixpkgs.follows = "nixpkgs"; + inputs.gomod2nix.inputs.flake-utils.follows = "flake-utils"; outputs = { self, nixpkgs, flake-utils, gomod2nix }: (flake-utils.lib.eachDefaultSystem From 5a337a0def015785b8c20f0439f683f936f8d96c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Sep 2023 08:46:55 +0200 Subject: [PATCH 5/5] add dependabot --- .github/dependabot.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..c507d44 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "weekly" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly"