From 58021f049ed100d9a9a04a80f19c3feec52407b3 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 20 Feb 2022 14:05:18 +0100 Subject: [PATCH 1/9] nix: init flake --- flake.lock | 27 ++++++++++++++++++++++++ flake.nix | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..489f126a0 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1642707142, + "narHash": "sha256-BzH6xhnkn52yF4+A8C8slWFQRMInS5VYlzrH/rN1bnA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6f07605b6916abee5435ea1145a7541299a87c1b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-21.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..c5ca04c63 --- /dev/null +++ b/flake.nix @@ -0,0 +1,60 @@ +{ + description = "go-centrifuge"; + + inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11; + + outputs = { self, nixpkgs }: + let + pkgs = nixpkgs.legacyPackages.x86_64-linux; + name = "go-centrifuge"; + version = "2.0"; + system = "x86_64-linux"; + in + { + + packages.${system} = { + go-centrifuge = pkgs.buildGoModule rec { + pname = name; + inherit version; + + src = ./.; + + vendorSha256 = "sha256-Yr1lxkeW9rvOR+tLn9YBE682hgatsLGRqsalhcz+r9Y="; + }; + + dockerContainer = + pkgs.dockerTools.buildImage { + name = "centrifugeio/${name}"; + tag = version; + + contents = self.defaultPackage.${system}; + + config = { + Env = [ + "PATH=/bin:$PATH" + ]; + ExposedPorts = { + "30333/tcp" = { }; + "9933/tcp" = { }; + "9944/tcp" = { }; + }; + Volumes = { + "/data" = { }; + }; + Entrypoint = [ "centrifuge" ]; + }; + }; + }; + + defaultPackage.${system} = self.packages.${system}.go-centrifuge; + + apps.${system}.centrifuge = { + type = "app"; + program = "${self.defaultPackage.${system}}/bin/centrifuge"; + }; + + defaultApp.${system} = self.apps.${system}.centrifuge; + + + }; +} From 06fa31859e012062684490fbb80c4e937571f348 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 20 Feb 2022 16:57:38 +0100 Subject: [PATCH 2/9] nix: further work --- flake.nix | 75 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/flake.nix b/flake.nix index c5ca04c63..a98ae6396 100644 --- a/flake.nix +++ b/flake.nix @@ -1,49 +1,70 @@ { description = "go-centrifuge"; - inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11; + inputs = { + nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11; + gitignore = { + url = github:hercules-ci/gitignore.nix; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; - outputs = { self, nixpkgs }: + outputs = { self, nixpkgs, gitignore }: let - pkgs = nixpkgs.legacyPackages.x86_64-linux; name = "go-centrifuge"; version = "2.0"; + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + + gitignoreFilter = gitignore.lib.gitignoreFilter; + + srcFilter = src: + let + srcIgnored = gitignoreFilter src; + in + path: type: + let + p = builtins.baseNameOf path; + in + srcIgnored path type + || (p == "flake.nix"); in { - packages.${system} = { - go-centrifuge = pkgs.buildGoModule rec { + go-centrifuge = pkgs.buildGoModule { pname = name; inherit version; - src = ./.; + src = pkgs.lib.cleanSourceWith { + src = ./.; + filter = srcFilter ./.; + name = "${name}-source"; + }; vendorSha256 = "sha256-Yr1lxkeW9rvOR+tLn9YBE682hgatsLGRqsalhcz+r9Y="; }; - dockerContainer = - pkgs.dockerTools.buildImage { - name = "centrifugeio/${name}"; - tag = version; - - contents = self.defaultPackage.${system}; - - config = { - Env = [ - "PATH=/bin:$PATH" - ]; - ExposedPorts = { - "30333/tcp" = { }; - "9933/tcp" = { }; - "9944/tcp" = { }; - }; - Volumes = { - "/data" = { }; - }; - Entrypoint = [ "centrifuge" ]; + dockerImage = pkgs.dockerTools.buildLayeredImage { + name = "centrifugeio/${name}"; + tag = version; + + contents = [ pkgs.busybox self.defaultPackage.${system} ]; + + config = { + # TODO what ports do we actually need? + ExposedPorts = { + # api + "8082/tcp" = { }; + # p2p + "38202/tcp" = { }; + }; + Volumes = { + "/data" = { }; }; + Entrypoint = [ "centrifuge" ]; }; + }; }; defaultPackage.${system} = self.packages.${system}.go-centrifuge; @@ -54,7 +75,5 @@ }; defaultApp.${system} = self.apps.${system}.centrifuge; - - }; } From 244065f3c3350d3c6c7fa38b7b39b1ef2e347458 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 20 Feb 2022 17:38:26 +0100 Subject: [PATCH 3/9] nix: fix source cleaning --- flake.lock | 21 +++++++++++++++++++++ flake.nix | 9 ++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/flake.lock b/flake.lock index 489f126a0..90e89d858 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,25 @@ { "nodes": { + "gitignore": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1635165013, + "narHash": "sha256-o/BdVjNwcB6jOmzZjOH703BesSkkS5O7ej3xhyO8hAY=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "5b9e0ff9d3b551234b4f3eb3983744fa354b17f1", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1642707142, @@ -18,6 +38,7 @@ }, "root": { "inputs": { + "gitignore": "gitignore", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index a98ae6396..8f8f1b2e9 100644 --- a/flake.nix +++ b/flake.nix @@ -22,13 +22,20 @@ srcFilter = src: let srcIgnored = gitignoreFilter src; + in path: type: let p = builtins.baseNameOf path; in srcIgnored path type - || (p == "flake.nix"); + && p != "flake.nix" + && p != "flake.lock" + && p != ".envrc" + && p != ".github" + && p != "CODE_OF_CONDUCT.md" + && p != "codecov.yml" + && p != "README.md"; in { packages.${system} = { From fa4e271fb433efb581a42fb2bdf8ec39f9126d5d Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 20 Feb 2022 17:45:09 +0100 Subject: [PATCH 4/9] nix: use builtins.any to simplify filtering --- flake.nix | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/flake.nix b/flake.nix index 8f8f1b2e9..fc4ef65a1 100644 --- a/flake.nix +++ b/flake.nix @@ -17,25 +17,22 @@ system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; - gitignoreFilter = gitignore.lib.gitignoreFilter; - srcFilter = src: let - srcIgnored = gitignoreFilter src; - + srcIgnored = gitignore.lib.gitignoreFilter src; + ignoreList = [ + "flake.nix" + "flake.lock" + ".envrc" + ".github" + "CODE_OF_CONDUCT.md" + "codecov.yml" + "README.md" + ]; in path: type: - let - p = builtins.baseNameOf path; - in srcIgnored path type - && p != "flake.nix" - && p != "flake.lock" - && p != ".envrc" - && p != ".github" - && p != "CODE_OF_CONDUCT.md" - && p != "codecov.yml" - && p != "README.md"; + && builtins.all (name: builtins.baseNameOf path != name) ignoreList; in { packages.${system} = { From 26c760802aac7a23399d367598987e2e48ed9029 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 20 Feb 2022 17:52:55 +0100 Subject: [PATCH 5/9] nix: sort ignoreList --- flake.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index fc4ef65a1..5ea53aaa0 100644 --- a/flake.nix +++ b/flake.nix @@ -21,13 +21,13 @@ let srcIgnored = gitignore.lib.gitignoreFilter src; ignoreList = [ - "flake.nix" - "flake.lock" ".envrc" ".github" "CODE_OF_CONDUCT.md" - "codecov.yml" "README.md" + "codecov.yml" + "flake.lock" + "flake.nix" ]; in path: type: From 9bc39a152f0d656e40f3430f863cb4a2b9d863c4 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sun, 20 Feb 2022 22:45:37 +0100 Subject: [PATCH 6/9] nix: remove busybox from container --- flake.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index 5ea53aaa0..a37184cd1 100644 --- a/flake.nix +++ b/flake.nix @@ -53,10 +53,9 @@ name = "centrifugeio/${name}"; tag = version; - contents = [ pkgs.busybox self.defaultPackage.${system} ]; + contents = [ self.defaultPackage.${system} ]; config = { - # TODO what ports do we actually need? ExposedPorts = { # api "8082/tcp" = { }; From c314958b7cbc6b43cf1fd093a2e013294471990f Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Tue, 22 Feb 2022 21:31:57 +0100 Subject: [PATCH 7/9] nix: add busybox to image --- flake.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index a37184cd1..dece0d40a 100644 --- a/flake.nix +++ b/flake.nix @@ -53,7 +53,10 @@ name = "centrifugeio/${name}"; tag = version; - contents = [ self.defaultPackage.${system} ]; + contents = [ + pkgs.busybox + self.defaultPackage.${system} + ]; config = { ExposedPorts = { From 5ae4cdeea381ec1e253705a9eef8013a1abe9ca5 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sat, 26 Feb 2022 11:07:13 +0100 Subject: [PATCH 8/9] nix: add commit hash to Docker image tag --- flake.nix | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/flake.nix b/flake.nix index dece0d40a..13775227f 100644 --- a/flake.nix +++ b/flake.nix @@ -15,6 +15,7 @@ version = "2.0"; system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; srcFilter = src: @@ -49,28 +50,36 @@ vendorSha256 = "sha256-Yr1lxkeW9rvOR+tLn9YBE682hgatsLGRqsalhcz+r9Y="; }; - dockerImage = pkgs.dockerTools.buildLayeredImage { - name = "centrifugeio/${name}"; - tag = version; + dockerImage = + let + # This evaluates to the first 6 digits of the git hash of this repo's HEAD + # commit, or to "dirty" if there are uncommitted changes. + commit-substr = builtins.substring 0 6 (self.rev or "dirty"); - contents = [ - pkgs.busybox - self.defaultPackage.${system} - ]; + tag = "${version}-${commit-substr}"; + in + pkgs.dockerTools.buildLayeredImage { + name = "centrifugeio/${name}"; + inherit tag; - config = { - ExposedPorts = { - # api - "8082/tcp" = { }; - # p2p - "38202/tcp" = { }; - }; - Volumes = { - "/data" = { }; + contents = [ + pkgs.busybox + self.defaultPackage.${system} + ]; + + config = { + ExposedPorts = { + # api + "8082/tcp" = { }; + # p2p + "38202/tcp" = { }; + }; + Volumes = { + "/data" = { }; + }; + Entrypoint = [ "centrifuge" ]; }; - Entrypoint = [ "centrifuge" ]; }; - }; }; defaultPackage.${system} = self.packages.${system}.go-centrifuge; From 6b135269a955f8d1370cc851a67a3f76c1279135 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Sat, 26 Feb 2022 11:20:35 +0100 Subject: [PATCH 9/9] nix: add devShell --- flake.nix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/flake.nix b/flake.nix index 13775227f..296db8a5c 100644 --- a/flake.nix +++ b/flake.nix @@ -61,6 +61,7 @@ pkgs.dockerTools.buildLayeredImage { name = "centrifugeio/${name}"; inherit tag; + created = builtins.substring 0 8 self.lastModifiedDate; contents = [ pkgs.busybox @@ -90,5 +91,9 @@ }; defaultApp.${system} = self.apps.${system}.centrifuge; + + devShell.${system} = pkgs.mkShellNoCC { + buildInputs = [ self.defaultPackage.${system} ]; + }; }; }