Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dynamic import #473

Merged
merged 35 commits into from Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
39d768c
Make the channel importer versions dynamic
dasJ Mar 29, 2022
e48ea62
Merge remote-tracking branch 'origin/main' into feat/dynamic-import
garbas Apr 7, 2022
02f2db1
few things fromt he list
garbas Apr 7, 2022
e3d3fc1
Decode nixosChannels in Elm
garbas Apr 7, 2022
de30398
Use nixosChannels that came via application flag
garbas Apr 12, 2022
72748d8
Merge remote-tracking branch 'origin/main' into feat/dynamic-import
garbas Apr 12, 2022
28a2d4d
read nixos channels in github action
garbas Apr 12, 2022
6a50ea0
defaultNixOSChannel should be calculated
garbas Apr 14, 2022
b45ccc0
add two pointers where the check should be added
garbas Apr 14, 2022
5ce774b
pass nixosChannels to flake-info and remove title, rather calculate it
garbas Apr 15, 2022
898cf31
Add NixosChannels struct validation and validation Error
ysndr Apr 15, 2022
b23fc2d
Read NIXOS_CHANNEL variable
ysndr Apr 15, 2022
a631802
Check channel
ysndr Apr 15, 2022
f14ff5f
Add channel struct to fix parsing NIXOS_CHANNELS
ysndr Apr 15, 2022
ecce987
Use `eachDefaultSystem` instead of listing them manually
ysndr Apr 17, 2022
638da42
Add individual dev shells for frontend and backend
ysndr Apr 17, 2022
893d01c
Update .github/workflows/import-to-elasticsearch.yml
garbas Apr 20, 2022
687cc09
use both development environments by default (as it was)
garbas Apr 20, 2022
ebb94a3
pkgs.lib → lib everywhere
garbas Apr 20, 2022
a7645ae
Update flake.nix
garbas Apr 20, 2022
50c4eb7
Merge remote-tracking branch 'origin/main' into feat/dynamic-import
garbas Apr 20, 2022
9fd5827
Merge branch 'feat/dynamic-import' of github.com:dasJ/nixos-search in…
garbas Apr 20, 2022
82bbcd3
typo
garbas Apr 20, 2022
0c15c2a
bumping version to test the changes to import-to-elasticsearch github…
garbas Apr 20, 2022
688e918
some invisibile characters needed to be removed
garbas Apr 20, 2022
d02f04b
typo
garbas Apr 20, 2022
0d3366e
will this work
garbas Apr 20, 2022
853b80c
typo
garbas Apr 20, 2022
1ac5d92
forgot the checkout step
garbas Apr 20, 2022
4602fe1
add some debugging
garbas Apr 20, 2022
e7f16f2
typo
garbas Apr 20, 2022
0c89759
read NIXOS_CHANNELS from environment not via argument
garbas Apr 21, 2022
fcb7da3
Merge remote-tracking branch 'origin/main' into feat/dynamic-import
garbas Apr 21, 2022
e2183ae
fix for the NIXOS_CHANNELS variable
garbas Apr 24, 2022
bed708c
Merge remote-tracking branch 'origin/main' into feat/dynamic-import
garbas Apr 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions .github/workflows/import-to-elasticsearch.yml
Expand Up @@ -7,15 +7,36 @@ on:

jobs:

nixos-channels:
runs-on: ubuntu-latest

outputs:
matrix: ${{ steps.nixos-channels.outputs.matrix }}

steps:
- name: Checking out the repository
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/common-setup
with:
CACHIX_SIGNING_KEY: ${{ secrets.CACHIX_SIGNING_KEY }}

- name: NixOS Channels
id: nixos-channels
run: |
nix build -L .#nixosChannels
channels="{\"channel\": $(< ./result)}"
echo $channels
echo "::set-output name=matrix::$channels"

import-nixpkgs:
needs: nixos-channels
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
channel:
- unstable
- 21.11
matrix: ${{ fromJSON(needs.nixos-channels.outputs.matrix) }}

env:
RUST_LOG: debug
Expand Down Expand Up @@ -46,7 +67,6 @@ jobs:
if: github.repository_owner == 'NixOS'



import-flakes:
runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
29
30
24 changes: 17 additions & 7 deletions flake-info/default.nix
@@ -1,5 +1,7 @@
{ pkgs ? import <nixpkgs> { } }: with pkgs;
rustPlatform.buildRustPackage rec {
{ pkgs ? import <nixpkgs> {}
, nixosChannels ? {}
}:
pkgs.rustPlatform.buildRustPackage rec {
name = "flake-info";
src = ./.;
cargoLock = {
Expand All @@ -8,11 +10,18 @@ rustPlatform.buildRustPackage rec {
"elasticsearch-8.0.0-alpha.1" = "sha256-gjmk3Q3LTAvLhzQ+k1knSp1HBwtqNiubjXNnLy/cS5M=";
};
};
nativeBuildInputs = [ pkg-config ];
buildInputs = [ openssl openssl.dev makeWrapper ]
++ lib.optional pkgs.stdenv.isDarwin [ libiconv darwin.apple_sdk.frameworks.Security ];
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs =
with pkgs; [
openssl
openssl.dev
makeWrapper
] ++ lib.optional pkgs.stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
];

checkInputs = [ pandoc ];
checkInputs = with pkgs; [ pandoc ];

NIXPKGS_PANDOC_FILTERS_PATH = "${pkgs.path + "/doc/build-aux/pandoc-filters"}";

Expand All @@ -24,6 +33,7 @@ rustPlatform.buildRustPackage rec {
postInstall = ''
wrapProgram $out/bin/flake-info \
--set NIXPKGS_PANDOC_FILTERS_PATH "${NIXPKGS_PANDOC_FILTERS_PATH}" \
--prefix PATH : ${pandoc}/bin
--set NIXOS_CHANNELS '${builtins.toJSON nixosChannels}' \
--prefix PATH : ${pkgs.pandoc}/bin
'';
}
43 changes: 43 additions & 0 deletions flake-info/src/bin/flake-info.rs
Expand Up @@ -5,11 +5,14 @@ use flake_info::data::import::{Kind, NixOption};
use flake_info::data::{self, Export, Nixpkgs, Source};
use flake_info::elastic::{ElasticsearchError, ExistsStrategy};
use flake_info::{commands, elastic};
use lazy_static::lazy_static;
use log::{debug, error, info, warn};
use semver::VersionReq;
use serde::Deserialize;
use sha2::Digest;
use std::path::{Path, PathBuf};
use std::ptr::hash;
use std::str::FromStr;
use std::{fs, io};
use structopt::{clap::ArgGroup, StructOpt};
use thiserror::Error;
Expand Down Expand Up @@ -198,6 +201,9 @@ enum FlakeInfoError {
#[error("Nix check failed: {0}")]
NixCheck(#[from] NixCheckError),

#[error("Nixos Channel `{0}` not among the allowed Channels set by NIXOS_CHANNELS ({:?}", .1.channels)]
UnknownNixOSChannel(String, NixosChannels),

#[error("Getting flake info caused an error: {0:?}")]
Flake(anyhow::Error),
#[error("Getting nixpkgs info caused an error: {0:?}")]
Expand Down Expand Up @@ -237,6 +243,8 @@ async fn run_command(
Ok((exports, ident))
}
Command::Nixpkgs { channel } => {
NIXOS_CHANNELS.check_channel(&channel)?;

let nixpkgs = Source::nixpkgs(channel)
.await
.map_err(FlakeInfoError::Nixpkgs)?;
Expand All @@ -251,6 +259,8 @@ async fn run_command(
Ok((exports, ident))
}
Command::NixpkgsArchive { source, channel } => {
NIXOS_CHANNELS.check_channel(&channel)?;

let ident = (
"nixos".to_string(),
channel.to_owned(),
Expand Down Expand Up @@ -404,3 +414,36 @@ async fn push_to_elastic(

Ok(())
}

/// Information about allowed and default nixos channels.
/// Typyically passed by environment variable NIXOS_CHANNELS.
/// Used to filter the input arguments for `flake-info nixpkgs` and `flake-info nixpkgs-archive`
#[derive(Clone, Debug, Deserialize)]
struct NixosChannels {
channels: Vec<Channel>,
}

#[derive(Clone, Debug, Deserialize)]
struct Channel {
branch: String
}

impl NixosChannels {
fn check_channel(&self, channel: &String) -> Result<(), FlakeInfoError> {
self.channels.iter().find(|c| &c.branch == channel).map_or_else(|| Ok(()), |_| Err(FlakeInfoError::UnknownNixOSChannel(channel.clone(), self.clone())))
}
}

impl FromStr for NixosChannels {
type Err=serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}

lazy_static! {
static ref NIXOS_CHANNELS: NixosChannels = std::env::var("NIXOS_CHANNELS")
.unwrap_or("".to_string())
.parse().unwrap();
}
17 changes: 17 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 96 additions & 13 deletions flake.nix
Expand Up @@ -6,36 +6,119 @@

inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixos-org-configurations.url = "github:NixOS/nixos-org-configurations";
inputs.nixos-org-configurations.flake = false;

outputs = { self
, nixpkgs
, flake-utils
, nixos-org-configurations
}:
flake-utils.lib.eachSystem
(with flake-utils.lib.system; [
x86_64-linux
i686-linux
x86_64-darwin
aarch64-linux
])
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
warnToUpgradeNix = pkgs.lib.warn "Please upgrade Nix to 2.7 or later.";
lib = nixpkgs.lib;
warnToUpgradeNix = lib.warn "Please upgrade Nix to 2.7 or later.";
version = lib.fileContents ./VERSION;
nixosChannels =
let
allChannels = (import "${nixos-org-configurations}/channels.nix").channels;
filteredChannels =
lib.filterAttrs
(n: v:
builtins.elem v.status ["beta" "stable" "rolling"] &&
lib.hasPrefix "nixos-" n &&
v ? variant && v.variant == "primary"
)
allChannels;
in
{
channels =
lib.mapAttrsToList
(n: v:
{
id = lib.removePrefix "nixos-" n;
status = v.status;
jobset =
builtins.concatStringsSep
"/"
(lib.init (lib.splitString "/" v.job));
branch = n;
}
)
filteredChannels;
default =
builtins.head
(builtins.sort (e1: e2: ! (builtins.lessThan e1 e2))
(builtins.map
(lib.removePrefix "nixos-")
(builtins.attrNames
(lib.filterAttrs (_: v: v.status == "stable") filteredChannels)
)
)
);
};
nixosChannelsFile = pkgs.runCommand "nixosChannels.json" {} ''
echo '${builtins.toJSON (builtins.map (c: c.id) nixosChannels.channels)}' > $out
'';

mkDevShell = { inputsFrom ? [], extraPackages ? [], extraShellHook ? "" }:
pkgs.mkShell {
inherit inputsFrom;
packages = extraPackages;
shellHook = ''
export NIXOS_CHANNELS='${builtins.toJSON nixosChannels}';
export ELASTICSEARCH_MAPPING_SCHEMA_VERSION="${version}";
'' + extraShellHook;
};
in rec {

packages.default = packages.flake-info;
packages.flake-info = import ./flake-info { inherit pkgs; };
packages.frontend = import ./frontend { inherit pkgs; };
packages.flake-info = import ./flake-info { inherit pkgs nixosChannels; };
packages.frontend = import ./frontend { inherit pkgs nixosChannels version; };
packages.nixosChannels = nixosChannelsFile;

devShells.default = mkDevShell {
inputsFrom = [
packages.flake-info
packages.frontend
];
extraPackages = [pkgs.rustfmt];
extraShellHook = ''
export RUST_SRC_PATH="${pkgs.rustPlatform.rustLibSrc}";
export NIXPKGS_PANDOC_FILTERS_PATH="${packages.flake-info.NIXPKGS_PANDOC_FILTERS_PATH}";
export PATH=$PWD/frontend/node_modules/.bin:$PATH

rm -rf frontend/node_modules
ln -sf ${packages.frontend.yarnPkg}/libexec/${(builtins.parseDrvName packages.frontend.name).name}/node_modules frontend/
echo "========================================================"
echo "= To develop the frontend run: cd frontend && yarn dev ="
echo "========================================================"
'';
};

devShells.default = pkgs.mkShell {
inputsFrom = builtins.attrValues packages;
shellHook = ''
devShells.flake-info = mkDevShell {
inputsFrom = [packages.flake-info];
extraPackages = [pkgs.rustfmt];
extraShellHook = ''
export RUST_SRC_PATH="${pkgs.rustPlatform.rustLibSrc}";
export NIXPKGS_PANDOC_FILTERS_PATH="${packages.flake-info.NIXPKGS_PANDOC_FILTERS_PATH}";
'';
};

devShells.frontend = mkDevShell {
inputsFrom = [packages.frontend] ;
extraShellHook = ''
export PATH=$PWD/frontend/node_modules/.bin:$PATH
rm -rf frontend/node_modules
ln -sf ${packages.frontend.yarnPkg}/libexec/${(builtins.parseDrvName packages.frontend.name).name}/node_modules frontend/
echo "========================================================"
echo "= To develop the frontend run: cd frontend && yarn dev ="
echo "========================================================"
'';
};

# XXX: for backwards compatibility
devShell = warnToUpgradeNix devShells.default;
defaultPackage = warnToUpgradeNix packages.default;
Expand Down
17 changes: 6 additions & 11 deletions frontend/default.nix
@@ -1,5 +1,6 @@
{ pkgs ? import <nixpkgs> { }
, version ? pkgs.lib.removeSuffix "\n" (builtins.readFile ../VERSION)
, nixosChannels
, version
}:
let
package = builtins.fromJSON (builtins.readFile ./package.json);
Expand Down Expand Up @@ -52,6 +53,9 @@ pkgs.stdenv.mkDerivation {
elm-analyse
]);

ELASTICSEARCH_MAPPING_SCHEMA_VERSION = version;
NIXOS_CHANNELS = builtins.toJSON nixosChannels;

configurePhase = pkgs.elmPackages.fetchElmDeps {
elmPackages = import ./elm-srcs.nix;
elmVersion = pkgs.elmPackages.elm.version;
Expand All @@ -66,7 +70,6 @@ pkgs.stdenv.mkDerivation {
buildPhase = ''
# Yarn writes cache directories etc to $HOME.
export HOME=$PWD/yarn_home
sed -i -e "s|process.env.ELASTICSEARCH_MAPPING_SCHEMA_VERSION|${version}|" src/index.js
yarn prod
'';

Expand All @@ -75,14 +78,6 @@ pkgs.stdenv.mkDerivation {
cp -R ./dist/* $out/
cp netlify.toml $out/
'';
shellHook = ''
rm -rf frontend/node_modules
ln -sf ${yarnPkg}/libexec/${package.name}/node_modules frontend/
export PATH=$PWD/frontend/node_modules/.bin:$PATH
export ELASTICSEARCH_MAPPING_SCHEMA_VERSION=${version}
echo "============================"
echo "= To develop the frontend run: cd frontend && yarn dev ="
echo "============================"
'';

passthru.yarnPkg = yarnPkg;
}