From cd676607b31fd9a508f9a0e02bb5b594ff864aa4 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Sat, 23 Jul 2022 18:11:03 +0200 Subject: [PATCH] Nix flakes (#343) --- .envrc | 2 +- .gitignore | 1 + Justfile | 3 +- README.md | 2 +- flake.lock | 94 ++++++++++++++++++++ flake.nix | 25 ++++++ nix/shell.nix | 98 +++++++++++++++++++++ nix/sources.json | 38 --------- nix/sources.nix | 174 -------------------------------------- nix/stack-integration.nix | 23 +++++ package.yaml | 4 +- shell.nix | 64 +------------- stack.yaml | 10 +-- 13 files changed, 253 insertions(+), 285 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/shell.nix delete mode 100644 nix/sources.json delete mode 100644 nix/sources.nix create mode 100644 nix/stack-integration.nix diff --git a/.envrc b/.envrc index 051d09d2..8392d159 100644 --- a/.envrc +++ b/.envrc @@ -1 +1 @@ -eval "$(lorri direnv)" +use flake \ No newline at end of file diff --git a/.gitignore b/.gitignore index 49d27f65..2923a020 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ fission.yaml* *.zip elm-stuff /.deploy* +/.direnv /.stack-work /build /diffuse diff --git a/Justfile b/Justfile index 32b5febf..e4a6b6ef 100644 --- a/Justfile +++ b/Justfile @@ -169,7 +169,8 @@ js-prod: vendor-js # @dev: build - just watch-wo-build & just server + # just watch-wo-build & just server + just server @doc-tests: diff --git a/README.md b/README.md index e66b4828..3f11da5c 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ In short: ### Building it yourself -This project uses [Nix](https://nixos.org/features.html) to manage the project's environment. If you'd like to build this project without Nix, check out the dependencies in the `shell.nix` file (most are available through Homebrew as well). +This project uses [Nix](https://nixos.org/features.html) to manage the project's environment. If you'd like to build this project without Nix, check out the dependencies in the `nix/shell.nix` file (most are available through Homebrew as well). ```shell diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..22657af5 --- /dev/null +++ b/flake.lock @@ -0,0 +1,94 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1656928814, + "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1656065134, + "narHash": "sha256-oc6E6ByIw3oJaIyc67maaFcnjYOz1mMcOtHxbEf9NwQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "bee6a7250dd1b01844a2de7e02e4df7d8a0a206c", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1658380158, + "narHash": "sha256-DBunkegKWlxPZiOcw3/SNIFg93amkdGIy2g0y/jDpHg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a65b5b3f5504b8b89c196aba733bdf2b0bd13c16", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1656401090, + "narHash": "sha256-bUS2nfQsvTQW2z8SK7oEFSElbmoBahOPtbXPm0AL3I4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "16de63fcc54e88b9a106a603038dd5dd2feb21eb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1658544517, + "narHash": "sha256-ipu69vA0a6AcWZqnHLzeRhvnVZbURXALHLIsqQWtJe4=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "4bd340885e39e0625fc6dda8a5a9d13c921ebb96", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..97a00bb4 --- /dev/null +++ b/flake.nix @@ -0,0 +1,25 @@ +{ + description = "diffuse"; + + + # Inputs + # ====== + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay.url = "github:oxalica/rust-overlay"; + }; + + + # Outputs + # ======= + + outputs = { self, nixpkgs, flake-utils, rust-overlay }: + flake-utils.lib.simpleFlake { + inherit self nixpkgs; + name = "diffuse"; + shell = ./nix/shell.nix; + preOverlays = [ (import rust-overlay) ]; + }; +} \ No newline at end of file diff --git a/nix/shell.nix b/nix/shell.nix new file mode 100644 index 00000000..1072b319 --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,98 @@ +{ pkgs ? import {} }: with pkgs; let + + # Rust + # ---- + + rust = rust-bin.stable.latest.default; + rustPlatform = makeRustPlatform { + cargo = rust; + rustc = rust; + }; + + # Trying to fix an issue with `watchexec` on MacOS + # https://github.com/h4llow3En/mac-notification-sys/issues/28 + # Nix uses old Apple SDKs? Not sure, but definitely a Nix issue. + watchexec = rustPlatform.buildRustPackage rec { + pname = "watchexec"; + version = "1.20.4"; + + cargoSha256 = "sha256-YM+Zm3wFp3Lsx5LmyjGwZywV/SZjriL6JMDO1l0tNf4="; + + src = fetchFromGitHub { + owner = pname; + repo = pname; + rev = "cli-v${version}"; + sha256 = "sha256-se3iqz+qjwf71wvHQhCWYryEdUc+kY0Q0ZTg4i1ayNI="; + }; + + nativeBuildInputs = lib.optionals + stdenv.isDarwin + (with darwin.apple_sdk_11_0.frameworks; [ Cocoa Foundation ]); + }; + + # Wraps + # ----- + # Inspired by https://www.tweag.io/blog/2022-06-02-haskell-stack-nix-shell/ + + stack-wrapped = symlinkJoin { + name = "stack"; + paths = [ stack ]; + buildInputs = [ makeWrapper ]; + postBuild = '' + wrapProgram $out/bin/stack \ + --add-flags "\ + --nix \ + --no-nix-pure \ + --nix-shell-file=nix/stack-integration.nix \ + " + ''; + }; + + # Dependencies + # ------------ + + deps = { + + tools = [ + curl + just + simple-http-server + # watchexec + ]; + + languages = [ + elmPackages.elm + elmPackages.elm-format + nodejs-18_x + nodePackages.pnpm + rust + stack-wrapped + ]; + + tauri = { + # Needed to build Tauri on Mac OS + # https://github.com/NixOS/nixpkgs/blob/master/pkgs/os-specific/darwin/apple-sdk/frameworks.nix + macOS = [ + darwin.apple_sdk.frameworks.AppKit + darwin.apple_sdk.frameworks.WebKit + libiconv + ]; + }; + + }; + +in + +mkShell { + + buildInputs = builtins.concatLists [ + deps.tools + deps.languages + + # Mac OS dependencies + (lib.optionals stdenv.isDarwin deps.tauri.macOS) + ]; + + NIX_PATH = "nixpkgs=" + path; + +} diff --git a/nix/sources.json b/nix/sources.json deleted file mode 100644 index 7f106d43..00000000 --- a/nix/sources.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "niv": { - "branch": "master", - "description": "Easy dependency management for Nix projects", - "homepage": "https://github.com/nmattia/niv", - "owner": "nmattia", - "repo": "niv", - "rev": "82e5cd1ad3c387863f0545d7591512e76ab0fc41", - "sha256": "090l219mzc0gi33i3psgph6s2pwsc8qy4lyrqjdj4qzkvmaj65a7", - "type": "tarball", - "url": "https://github.com/nmattia/niv/archive/82e5cd1ad3c387863f0545d7591512e76ab0fc41.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, - "nixpkgs": { - "branch": "nixpkgs-unstable", - "description": "Nix Packages collection", - "homepage": null, - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "17b62c338f2a0862a58bb6951556beecd98ccda9", - "sha256": "1yzbc85m9vbhsfprljzjkkskh9sxchid9m28wkgwsckqnf47r911", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/17b62c338f2a0862a58bb6951556beecd98ccda9.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, - "old-packages": { - "branch": "nixos-21.05", - "description": "Nix Packages collection", - "homepage": "", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "530a53dcbc9437363471167a5e4762c5fcfa34a1", - "sha256": "11d9m0c4r4kpr3jx3cqblw6ld4d8dwcfv1lk68ipp4c87knwv7fb", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/530a53dcbc9437363471167a5e4762c5fcfa34a1.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - } -} diff --git a/nix/sources.nix b/nix/sources.nix deleted file mode 100644 index 1938409d..00000000 --- a/nix/sources.nix +++ /dev/null @@ -1,174 +0,0 @@ -# This file has been generated by Niv. - -let - - # - # The fetchers. fetch_ fetches specs of type . - # - - fetch_file = pkgs: name: spec: - let - name' = sanitizeName name + "-src"; - in - if spec.builtin or true then - builtins_fetchurl { inherit (spec) url sha256; name = name'; } - else - pkgs.fetchurl { inherit (spec) url sha256; name = name'; }; - - fetch_tarball = pkgs: name: spec: - let - name' = sanitizeName name + "-src"; - in - if spec.builtin or true then - builtins_fetchTarball { name = name'; inherit (spec) url sha256; } - else - pkgs.fetchzip { name = name'; inherit (spec) url sha256; }; - - fetch_git = name: spec: - let - ref = - if spec ? ref then spec.ref else - if spec ? branch then "refs/heads/${spec.branch}" else - if spec ? tag then "refs/tags/${spec.tag}" else - abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"; - in - builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; }; - - fetch_local = spec: spec.path; - - fetch_builtin-tarball = name: throw - ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`. - $ niv modify ${name} -a type=tarball -a builtin=true''; - - fetch_builtin-url = name: throw - ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`. - $ niv modify ${name} -a type=file -a builtin=true''; - - # - # Various helpers - # - - # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695 - sanitizeName = name: - ( - concatMapStrings (s: if builtins.isList s then "-" else s) - ( - builtins.split "[^[:alnum:]+._?=-]+" - ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name) - ) - ); - - # The set of packages used when specs are fetched using non-builtins. - mkPkgs = sources: system: - let - sourcesNixpkgs = - import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; }; - hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; - hasThisAsNixpkgsPath = == ./.; - in - if builtins.hasAttr "nixpkgs" sources - then sourcesNixpkgs - else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then - import {} - else - abort - '' - Please specify either (through -I or NIX_PATH=nixpkgs=...) or - add a package called "nixpkgs" to your sources.json. - ''; - - # The actual fetching function. - fetch = pkgs: name: spec: - - if ! builtins.hasAttr "type" spec then - abort "ERROR: niv spec ${name} does not have a 'type' attribute" - else if spec.type == "file" then fetch_file pkgs name spec - else if spec.type == "tarball" then fetch_tarball pkgs name spec - else if spec.type == "git" then fetch_git name spec - else if spec.type == "local" then fetch_local spec - else if spec.type == "builtin-tarball" then fetch_builtin-tarball name - else if spec.type == "builtin-url" then fetch_builtin-url name - else - abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; - - # If the environment variable NIV_OVERRIDE_${name} is set, then use - # the path directly as opposed to the fetched source. - replace = name: drv: - let - saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; - ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; - in - if ersatz == "" then drv else - # this turns the string into an actual Nix path (for both absolute and - # relative paths) - if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}"; - - # Ports of functions for older nix versions - - # a Nix version of mapAttrs if the built-in doesn't exist - mapAttrs = builtins.mapAttrs or ( - f: set: with builtins; - listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) - ); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 - range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 - stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 - stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); - concatMapStrings = f: list: concatStrings (map f list); - concatStrings = builtins.concatStringsSep ""; - - # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331 - optionalAttrs = cond: as: if cond then as else {}; - - # fetchTarball version that is compatible between all the versions of Nix - builtins_fetchTarball = { url, name ? null, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchTarball; - in - if lessThan nixVersion "1.12" then - fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) - else - fetchTarball attrs; - - # fetchurl version that is compatible between all the versions of Nix - builtins_fetchurl = { url, name ? null, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchurl; - in - if lessThan nixVersion "1.12" then - fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) - else - fetchurl attrs; - - # Create the final "sources" from the config - mkSources = config: - mapAttrs ( - name: spec: - if builtins.hasAttr "outPath" spec - then abort - "The values in sources.json should not have an 'outPath' attribute" - else - spec // { outPath = replace name (fetch config.pkgs name spec); } - ) config.sources; - - # The "config" used by the fetchers - mkConfig = - { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null - , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile) - , system ? builtins.currentSystem - , pkgs ? mkPkgs sources system - }: rec { - # The sources, i.e. the attribute set of spec name to spec - inherit sources; - - # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers - inherit pkgs; - }; - -in -mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } diff --git a/nix/stack-integration.nix b/nix/stack-integration.nix new file mode 100644 index 00000000..86ea71eb --- /dev/null +++ b/nix/stack-integration.nix @@ -0,0 +1,23 @@ +{ ghc }: +with (import {}); +let + macos = + if stdenv.isDarwin then + [ + darwin.apple_sdk.frameworks.CoreServices + darwin.apple_sdk.frameworks.Foundation + darwin.apple_sdk.frameworks.Cocoa + ] + else + []; +in +haskell.lib.buildStackProject { + inherit ghc; + name = "diffuse"; + buildInputs = [ + lzma + openssl + postgresql + zlib + ] ++ macos; +} \ No newline at end of file diff --git a/package.yaml b/package.yaml index a59adf18..ba52cb44 100644 --- a/package.yaml +++ b/package.yaml @@ -10,9 +10,9 @@ default-extensions: dependencies: - aeson - base >= 4.7 && < 5 - - bytestring == 0.10.* + - bytestring == 0.* - cmark == 0.6.* - - flow == 1.* + - flow == 2.* - protolude - shikensu - text == 1.* diff --git a/shell.nix b/shell.nix index 4e24463e..963948b4 100644 --- a/shell.nix +++ b/shell.nix @@ -1,61 +1,3 @@ -let - - sources = import ./nix/sources.nix; - pkgs = import sources.nixpkgs { overlays = [ (import rustOverlay) ]; }; - - rustOverlay = builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"; - - # Dependencies - # ------------ - - deps = { - - tools = [ - pkgs.curl - pkgs.just - pkgs.simple-http-server - pkgs.watchexec - ]; - - languages = [ - pkgs.elmPackages.elm - pkgs.elmPackages.elm-format - pkgs.haskellPackages.stack - pkgs.nodejs-18_x - pkgs.nodePackages.pnpm - - pkgs.rust-bin.stable.latest.default - # (pkgs.rust-bin.stable.latest.default.override { - # targets = - # if pkgs.stdenv.isDarwin then - # [ "aarch64-apple-darwin" "x86_64-apple-darwin" ] - # else - # []; - # }) - ]; - - tauri = { - # Needed to build Tauri on Mac OS - # https://github.com/NixOS/nixpkgs/blob/master/pkgs/os-specific/darwin/apple-sdk/frameworks.nix - macOS = [ - pkgs.darwin.apple_sdk.frameworks.AppKit - pkgs.darwin.apple_sdk.frameworks.WebKit - pkgs.libiconv - ]; - }; - - }; - -in - - pkgs.mkShell { - - buildInputs = builtins.concatLists [ - deps.tools - deps.languages - - # Mac OS dependencies - (pkgs.lib.optionals pkgs.stdenv.isDarwin deps.tauri.macOS) - ]; - - } +(import (fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/b4a34015c698c7793d592d66adbab377907a2be8.tar.gz"; +}) { src = ./.; }).shellNix \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 0397fa5b..4a84cda3 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,10 +1,6 @@ -resolver: nightly-2021-12-26 +resolver: lts-19.16 recommend-stack-upgrade: false - -# nix: -# enable: true +allow-newer: true extra-deps: - - aeson-2.0.2.0 - - protolude-0.2.4 - - shikensu-0.4.0 + - protolude-0.3.2 -- 2.51.2