# Copyright 2019 PrivateStorage.io, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: 2

jobs:
  test:
    docker:
      - image: "nixorg/nix:circleci"
    steps:
      - "checkout"

      - run:
          name: "Run Tests"
          command: |
            export NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/$(cat nixpkgs.rev).tar.gz
            nix-build nixos/unit-tests.nix && cat result

  system-tests-driver:
    # Cannot actually run the system tests on CircleCI but we can build
    # everything that makes them up.  This by itself can catch a lot of
    # problems.
    docker:
      - image: "nixorg/nix:circleci"
    steps:
      - "checkout"

      - restore_cache:
          # Get all of Nix's state relating to the particular revision of
          # nixpkgs we're using.  It will always be the same.  CircleCI
          # artifacts and nixpkgs store objects are probably mostly hosted in
          # the same place (S3) so there's not a lot of difference for
          # anything that's pre-built.  For anything we end up building
          # ourselves, though, this saves us all of the build time (less the
          # download time).
          #
          # Read about caching dependencies: https://circleci.com/docs/2.0/caching/
          name: "Restore Nix Store Paths"
          keys:
            # Construct cache keys that allow sharing as long as nixpkgs
            # revision is unchanged.
            #
            # If nixpkgs changes then potentially a lot of cached packages for
            # the base system will be invalidated so we may as well drop them
            # and make a new cache with the new packages.
            - privatestorageio-nix-store-v1-{{ checksum "nixpkgs.rev" }}
            - privatestorageio-nix-store-v1-

      - run:
          name: "Run Tests"
          command: |
            export NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/$(cat nixpkgs.rev).tar.gz
            nix-build --max-jobs 1 nixos/system-tests.nix -A driver

      - save_cache:
          name: "Cache Nix Store Paths"
          key: privatestorageio-nix-store-v1-{{ checksum "nixpkgs.rev" }}
          paths:
            - "/nix"

  build:
    docker:
      - image: "nixorg/nix:circleci"
    steps:
      - "checkout"

      - restore_cache:
          # See comments for nix store caching in `build` job.
          name: "Restore Nix Store Paths"
          keys:
            - privatestorageio-docs-nix-store-v1-{{ checksum "nixpkgs.rev" }}
            - privatestorageio-docs-nix-store-v1-

      - run:
          name: "Nix Build"
          command: |
            export NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/$(cat nixpkgs.rev).tar.gz
            nix-build docs.nix

      - save_cache:
          name: "Cache Nix Store Paths"
          key: privatestorageio-docs-nix-store-v1-{{ checksum "nixpkgs.rev" }}
          paths:
            - "/nix"

      - store_artifacts:
          path: "result/docs"
          destination: "docs"

workflows:
  version: 2
  everything:
    jobs:
      - "test"
      - "build"
      - "system-tests-driver"
