From f12429bb3635803223a22fc8fcffa3304b074584 Mon Sep 17 00:00:00 2001 From: Josh Robson Chase Date: Fri, 17 Dec 2021 15:52:09 -0500 Subject: [PATCH] chore(nix): refactor for flake support and add package build (#220) * chore(nix): refactor for flake support and add package build Pins nixpkgs to a known working version for the flake. Things should be unchanged for non-flake-users. * chore(nix): trim unnecessary deps from shell.nix * chore(nix): relocate most nix files to the nix directory For flake and direnv users, this is a small refactor that won't be noticeable aside from the locations of the files for editing. For anyone who was previously using `nix-build` and `nix-shell`, they'll now have to `nix-build ./nix` and `nix-shell ./nix/shell.nix`. Co-authored-by: Eliza Weisman --- .envrc | 6 ++++- .gitignore | 4 +++- default.nix | 37 ---------------------------- flake.lock | 43 +++++++++++++++++++++++++++++++++ flake.nix | 24 +++++++++++++++++++ nix/default.nix | 2 ++ nix/shell.nix | 19 +++++++++++++++ nix/tokio-console.nix | 56 +++++++++++++++++++++++++++++++++++++++++++ shell.nix | 19 --------------- 9 files changed, 152 insertions(+), 58 deletions(-) delete mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/default.nix create mode 100644 nix/shell.nix create mode 100644 nix/tokio-console.nix delete mode 100644 shell.nix diff --git a/.envrc b/.envrc index 2493a157f..772cf7d12 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,5 @@ -use nix; \ No newline at end of file +if command -v use_flake 2>&1 >/dev/null; then + use flake; +else + use nix ./nix/shell.nix; +fi diff --git a/.gitignore b/.gitignore index 112a6b58d..19edc1803 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -target +/.direnv **/target +**/result +**/*.swp diff --git a/default.nix b/default.nix deleted file mode 100644 index 48288554f..000000000 --- a/default.nix +++ /dev/null @@ -1,37 +0,0 @@ -scope@{ pkgs ? import { } }: - -pkgs.buildEnv { - name = "console-env"; - paths = with pkgs; - [ - git - bash - direnv - binutils - stdenv - bashInteractive - docker - cacert - gcc - cmake - rustup - pkg-config - openssl - protobuf - docker - (glibcLocales.override { locales = [ "en_US.UTF-8" ]; }) - ] ++ lib.optional stdenv.isDarwin [ Security libiconv ]; - passthru = with pkgs; { - PROTOC = "${protobuf}/bin/protoc"; - PROTOC_INCLUDE = "${protobuf}/include"; - LOCALE_ARCHIVE = "${glibcLocales}/lib/locale/locale-archive"; - LC_ALL = "en_US.UTF-8"; - OPENSSL_DIR = "${openssl.dev}"; - OPENSSL_LIB_DIR = "${openssl.out}/lib"; - SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; - GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt"; - CURL_CA_BUNDLE = "${cacert}/etc/ca-bundle.crt"; - CARGO_TERM_COLOR = "always"; - RUST_BACKTRACE = "full"; - }; -} diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..f5660b1ca --- /dev/null +++ b/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1638122382, + "narHash": "sha256-sQzZzAbvKEqN9s0bzWuYmRaA03v40gaJ4+iL1LXjaeI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "74f7e4319258e287b0f9cb95426c9853b282730b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1639752787, + "narHash": "sha256-07kSWzpKtAzVvYyVdZ8MYUJDsU/G8IBu9USq0pNEHek=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "28abc4e43a24d28729509e2d83f5c4f3b3418189", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "release-21.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..0d956e7a4 --- /dev/null +++ b/flake.nix @@ -0,0 +1,24 @@ +{ + description = "The Tokio console: a debugger for async Rust."; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/release-21.11"; + flake-utils = { + url = "github:numtide/flake-utils"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + tokio-console = import ./nix { inherit pkgs; }; + devShell = import ./nix/shell.nix { inherit pkgs; }; + in + { + inherit devShell; + packages = { inherit tokio-console; }; + defaultPackage = tokio-console; + }); +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 000000000..2fc0fa8d6 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,2 @@ +{ pkgs ? import { } }: +pkgs.callPackage ./tokio-console.nix { } diff --git a/nix/shell.nix b/nix/shell.nix new file mode 100644 index 000000000..31229d5bb --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,19 @@ +scope@{ pkgs ? import { } }: +with pkgs; +let + tokio-console = import ./default.nix { inherit pkgs; }; + + env = buildEnv { + name = "console-env"; + paths = [ ] + ++ lib.optional stdenv.isDarwin libiconv + ++ tokio-console.buildInputs + ++ tokio-console.nativeBuildInputs; + }; +in +mkShell { + buildInputs = [ env ]; + + CARGO_TERM_COLOR = "always"; + RUST_BACKTRACE = "full"; +} diff --git a/nix/tokio-console.nix b/nix/tokio-console.nix new file mode 100644 index 000000000..b6f302153 --- /dev/null +++ b/nix/tokio-console.nix @@ -0,0 +1,56 @@ +{ lib +, protobuf +, rustPlatform +, nix-gitignore +}: +let + inherit (nix-gitignore) gitignoreFilterPure withGitignoreFile; + # Workaround for the builtins.filterSource issue mentioned in + # https://nixos.org/manual/nix/unstable/expressions/builtins.html + # Since this might be built from a flake, the source path may be a store path, + # so we need to provide our own version of gitignoreSource that avoids + # builtins.filterSource in favor of builtins.path. + gitignoreSource = patterns: path: + builtins.path { + filter = gitignoreFilterPure (_: _: true) (withGitignoreFile patterns path) path; + path = path; + name = "src"; + }; + + # Ignore some extra things that don't factor into the main build to help with + # caching. + extraIgnores = '' + /.envrc + /*.nix + /flake.* + /netlify.toml + /.github + /assets + /*.md + /.gitignore + /LICENSE + ''; + + src = gitignoreSource extraIgnores ../.; + + cargoTOML = lib.importTOML "${src}/console/Cargo.toml"; +in +rustPlatform.buildRustPackage rec { + pname = cargoTOML.package.name; + version = cargoTOML.package.version; + + nativeBuildInputs = [ + protobuf + ]; + + inherit src; + + cargoLock = { + lockFile = "${src}/Cargo.lock"; + }; + + meta = { + inherit (cargoTOML.package) description homepage license; + maintainers = cargoTOML.package.authors; + }; +} diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 57271af7c..000000000 --- a/shell.nix +++ /dev/null @@ -1,19 +0,0 @@ -scope@{ pkgs ? import { } }: - -let env = (import ./default.nix scope); - -in with pkgs; -mkShell { - PROTOC = "${protobuf}/bin/protoc"; - PROTOC_INCLUDE = "${protobuf}/include"; - LOCALE_ARCHIVE = "${glibcLocales}/lib/locale/locale-archive"; - LC_ALL = "en_US.UTF-8"; - OPENSSL_DIR = "${openssl.dev}"; - OPENSSL_LIB_DIR = "${openssl.out}/lib"; - SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; - GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt"; - CURL_CA_BUNDLE = "${cacert}/etc/ca-bundle.crt"; - CARGO_TERM_COLOR = "always"; - RUST_BACKTRACE = "1"; - buildInputs = [ (import ./default.nix { inherit pkgs; }) ]; -}