From 8d9cc82acc925dc01a0dc1c0164b0e4d03b4c29a Mon Sep 17 00:00:00 2001 From: Malo Bourgon Date: Wed, 13 Jan 2021 17:13:58 -0800 Subject: [PATCH] Update to use Nix flakes --- .github/workflows/nix.yml | 26 ++++-- default.nix | 31 +++---- flake.lock | 75 +++++++++++++++++ flake.nix | 41 +++++++++ nix/sources.json | 26 ------ nix/sources.nix | 171 -------------------------------------- shell.nix | 21 +++-- 7 files changed, 153 insertions(+), 238 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix delete mode 100644 nix/sources.json delete mode 100644 nix/sources.nix diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml index ec706fb..e7896ba 100644 --- a/.github/workflows/nix.yml +++ b/.github/workflows/nix.yml @@ -4,20 +4,28 @@ on: push: jobs: build-and-cache: - strategy: - matrix: - os: [ubuntu-latest, macos-latest] - runs-on: ${{ matrix.os }} + runs-on: macos-latest steps: - - uses: actions/checkout@v2 - - uses: cachix/install-nix-action@v11 + - name: Checkout repo + uses: actions/checkout@v2 + + - name: Install Nix + uses: cachix/install-nix-action@v12 with: + install_url: https://github.com/numtide/nix-flakes-installer/releases/download/nix-2.4pre20201221_9fab14a/install extra_nix_config: | trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= substituters = https://cache.nixos.org https://hydra.iohk.io https://iohk.cachix.org - - uses: cachix/cachix-action@v6 + experimental-features = nix-command flakes + + - name: Setup Cachix + uses: cachix/cachix-action@v8 with: name: malo signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' - - run: nix-build -A prefmanager.components.exes.prefmanager - - run: nix-shell --run "echo OK" + + - name: Build and cache + run: | + nix build . + nix run . -- --help + nix develop . diff --git a/default.nix b/default.nix index d043ea6..8447762 100644 --- a/default.nix +++ b/default.nix @@ -1,21 +1,10 @@ -{ # Fetch the latest haskell.nix and import its default.nix - haskellNix ? import (import nix/sources.nix)."haskell.nix" {} - -# haskell.nix provides access to the nixpkgs pins which are used by our CI, -# hence you will be more likely to get cache hits when using these. -# But you can also just use your own, e.g. ''. -, nixpkgsSrc ? haskellNix.sources.nixpkgs-2009 - -# haskell.nix provides some arguments to be passed to nixpkgs, including some -# patches and also the haskell.nix functionality itself as an overlay. -, nixpkgsArgs ? haskellNix.nixpkgsArgs - -# import nixpkgs with overlays -, pkgs ? import nixpkgsSrc nixpkgsArgs -}: pkgs.haskell-nix.project { - # 'cleanGit' cleans a source directory based on the files known by git - src = pkgs.haskell-nix.haskellLib.cleanGit { - name = "prefmanager"; - src = ./.; - }; -} +# See https://nixos.wiki/wiki/Flakes#Using_flakes_project_from_a_legacy_Nix +(import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; } +) { + src = ./.; +}).defaultNix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..bd00009 --- /dev/null +++ b/flake.lock @@ -0,0 +1,75 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1606424373, + "narHash": "sha256-oq8d4//CJOrVj+EcOaSXvMebvuTkmBJuT5tzlfewUnQ=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "99f1c2157fba4bfe6211a321fd0ee43199025dbf", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1610051610, + "narHash": "sha256-U9rPz/usA1/Aohhk7Cmc2gBrEEKRzcW4nwPWMPwja4Y=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "3982c9903e93927c2164caa727cd3f6a0e6d14cc", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "haskellNix": { + "locked": { + "lastModified": 1610500541, + "narHash": "sha256-XedfI5fERgPeIbJTMkd6AH1lJGXv28CxBYMEjzAfrJc=", + "owner": "input-output-hk", + "repo": "haskell.nix", + "rev": "faaca6735566d8707ffcc812ec0da964cded27f6", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "haskell.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1610575970, + "narHash": "sha256-cchAnKyFNOXt685FhephDAz1cjule36OOEizB2hAKHw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6e83386e825857ec11b786cb58425a5d57ad2716", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "haskellNix": "haskellNix", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..7286f7c --- /dev/null +++ b/flake.nix @@ -0,0 +1,41 @@ +{ + description = "Tool for working with macOS defaults."; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + haskellNix.url = "github:input-output-hk/haskell.nix"; + flake-compat = { url = "github:edolstra/flake-compat"; flake = false; }; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, haskellNix, flake-utils, ... }: + flake-utils.lib.eachSystem [ "x86_64-darwin" ] (system: let + pkgs = import nixpkgs { inherit system; overlays = [ haskellNix.overlay ]; }; + name = "prefmanager"; + compiler = "ghc884"; + project = pkgs.haskell-nix.project' { + inherit name; + src = self; + compiler-nix-name = compiler; + }; + components = project.hsPkgs.${name}.components; + in rec { + # Built by `nix build .` + defaultPackage = components.exes.${name}; + packages.${name} = defaultPackage; + + # Run `prefmanager` with `nix run .` + defaultApp = { type = "app"; program = components.exes.${name}.exePath; }; + apps.${name} = defaultApp; + + # This is used by `nix develop .` + devShell = project.shellFor { + buildInputs = [ + pkgs.haskell.packages.${compiler}.haskell-language-server + pkgs.haskell.packages.${compiler}.implicit-hie + pkgs.stack + ]; + }; + } + ); +} diff --git a/nix/sources.json b/nix/sources.json deleted file mode 100644 index 452ac60..0000000 --- a/nix/sources.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "haskell.nix": { - "branch": "master", - "description": "Alternative Haskell Infrastructure for Nixpkgs", - "homepage": "https://input-output-hk.github.io/haskell.nix", - "owner": "input-output-hk", - "repo": "haskell.nix", - "rev": "6134e66e1decf2155ba500dd5de00adb06e2cdee", - "sha256": "1w6mvlblszl3spx46m3a4bz13xah18gkznzpk0fzkmfizasv7lgi", - "type": "tarball", - "url": "https://github.com/input-output-hk/haskell.nix/archive/6134e66e1decf2155ba500dd5de00adb06e2cdee.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": "502845c3e31ef3de0e424f3fcb09217df2ce6df6", - "sha256": "0fcqpsy6y7dgn0y0wgpa56gsg0b0p8avlpjrd79fp4mp9bl18nda", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/502845c3e31ef3de0e424f3fcb09217df2ce6df6.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 b796fff..0000000 --- a/nix/sources.nix +++ /dev/null @@ -1,171 +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 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/shell.nix b/shell.nix index d4773e4..d8a08d0 100644 --- a/shell.nix +++ b/shell.nix @@ -1,11 +1,10 @@ -let - pkgs = import (import ./nix/sources.nix).nixpkgs {}; - hsPkgs = import ./default.nix {}; - compiler-name = hsPkgs.prefmanager.project.pkg-set.config.compiler.nix-name; -in hsPkgs.shellFor { - buildInputs = [ - pkgs.haskell.packages.${compiler-name}.haskell-language-server - pkgs.stack - pkgs.niv - ]; -} +# See https://nixos.wiki/wiki/Flakes#Using_flakes_project_from_a_legacy_Nix +(import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; } +) { + src = ./.; +}).shellNix