From cf497ce20b90797ad43a7b63d651844a2d9cfcb1 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 12 Apr 2023 20:00:50 +0300 Subject: [PATCH] add flake with cabal --- README.md | 10 ++++++++ cabal.project | 2 ++ flake.lock | 62 +++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/README.md b/README.md index d2996fe..ab0d589 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,16 @@ If you are building with stack then use `stack build --flag telegram-bot-simple: Contributions are welcome! Feel free to ping me on GitHub, file an issue or submit a PR :) +### Nix + +You can use a [Nix flake](https://nixos.wiki/wiki/Flakes) from this repo to get several development tools. + +1. [Enable flakes](https://nixos.wiki/wiki/Flakes#Enable_flakes). + +2. Run `nix develop`. This command will make available `cabal`, `ghc`, and `haskell-language-server`. + +3. Run `cabal run example-echo-bot` to start a bot. + ## Compatibility | telegram-bot-api | telegram-bot-simple | diff --git a/cabal.project b/cabal.project index 882c428..8062577 100644 --- a/cabal.project +++ b/cabal.project @@ -1,3 +1,5 @@ packages: telegram-bot-api/ telegram-bot-simple/ + +flags: +examples \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ba9aecf --- /dev/null +++ b/flake.lock @@ -0,0 +1,62 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1681184417, + "narHash": "sha256-hEYKxuO9gQkeCdyhgvH/Jhs2bkAFyUBALCFiaT5IQE0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1fb781f4a148c19e9da1d35a4cbe15d0158afc4e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1fb781f4a148c19e9da1d35a4cbe15d0158afc4e", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..67f6aee --- /dev/null +++ b/flake.nix @@ -0,0 +1,66 @@ +{ + inputs = { + flake-utils.url = "github:numtide/flake-utils/cfacdce06f30d2b68473a46042957675eebb3401"; + nixpkgs.url = "github:NixOS/nixpkgs/1fb781f4a148c19e9da1d35a4cbe15d0158afc4e"; + }; + outputs = inputs: inputs.flake-utils.lib.eachDefaultSystem (system: + let + pkgs = inputs.nixpkgs.legacyPackages.${system}; + + telegram-bot-api = "telegram-bot-api"; + telegram-bot-simple = "telegram-bot-simple"; + + systemDepends = [ pkgs.zlib ]; + override = { + overrides = self: super: + let inherit (pkgs.haskell.lib) overrideCabal; in + { + "${telegram-bot-api}" = overrideCabal + (super.callCabal2nix telegram-bot-api ./${telegram-bot-api} { }) + (x: { librarySystemDepends = systemDepends ++ (x.librarySystemDepends or [ ]); }); + "${telegram-bot-simple}" = overrideCabal + (self.callCabal2nix telegram-bot-simple ./${telegram-bot-simple} { + "${telegram-bot-api}" = self."${telegram-bot-api}"; + }) + (x: { librarySystemDepends = systemDepends ++ (x.librarySystemDepends or [ ]); }); + }; + }; + + ghcVersion = "ghc927"; + hpkgs = pkgs.haskell.packages.${ghcVersion}; + + getHaskellPackagesDeps = packages_: + with pkgs.lib.lists; pkgs.lib.lists.unique ( + subtractLists packages_ ( + concatLists (map + (package: + __filter pkgs.lib.attrsets.isDerivation (concatLists ( + __attrValues package.getCabalDeps + ))) + packages_) + ) + ); + + ghcForPackages = hpkgs_: override_: packageNames_: + (hpkgs_.override override_).ghcWithPackages (ps: + getHaskellPackagesDeps (map (x: ps.${x}) packageNames_) + ); + + ghc = ghcForPackages hpkgs override [ telegram-bot-api telegram-bot-simple ]; + + tools = [ + hpkgs.cabal-install + # ghc should go before haskell-language-server - https://github.com/NixOS/nixpkgs/issues/225895 + ghc + hpkgs.haskell-language-server + ]; + + devShells.default = pkgs.mkShell { + buildInputs = tools; + shellHook = "export LANG=C.utf8"; + }; + in + { + inherit devShells; + }); +}