VMUKZQZRR5NP7IVLXQ4RYGIHJOXB6MKS4YIIVEFU7S4OEK4QSFVAC
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Very simple HTTP GET
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
// HERE WE CHANGE THE URL WE FETCH
curl_easy_setopt(curl, CURLOPT_URL, "https://wttr.in/Delft");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// HERE WE PRETEND TO BE CURL, OTHERWISE WE GET HTML
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
{
description = "Simple flake for simple libcurl example";
# nixpkgs is the package repository for the Nix package manager
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
outputs = { self, nixpkgs }: let
# In this simple example we choose to build for "x86_64-linux" only
pkgs = nixpkgs.legacyPackages.x86_64-linux;
# Declare our own package
wttr-delft = pkgs.stdenv.mkDerivation rec {
name = "wttr-delft";
src = builtins.path { path = ../src; name = name; };
buildInputs = [
pkgs.curl.dev
];
buildPhase = "gcc -lcurl -o wttr-delft ./simple.c";
installPhase = "mkdir -p $out/bin; install -t $out/bin wttr-delft";
};
in
# These are the flake outputs, i.e. what we can consume
{
packages.x86_64-linux = {
default = wttr-delft;
};
};
}
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1679396235,
"narHash": "sha256-RjmNVFuZQ2e6u35B98JcY9IzVDtZb3d4QcbtfLtNWkE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "008ce261a7e3c532e792cb8e39482f2cc1b192f5",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}
#!/usr/bin/env bash
# shellcheck disable=SC2288
set -Eeuo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/../bash/libdemo.sh"
h Making our own wttr-delft package
, For some reason, we want to have a simple C package that explicitly gets the weather in Delft.
, We\'re going to use \'libcurl\' for this, and build the executable using nix.
h The source code
x pygmentize "$(dirname "${BASH_SOURCE[0]}")/../src/simple.c"
h Using a Nix flake
, The modern \(experimental, but recommended\) way to use Nix is to use \'flakes\':
,
, Flakes are the unit for packaging Nix code in a reproducible and discoverable way.
, They can have dependencies on other flakes, making it possible to have multi-repository Nix projects.
, A flake is a filesystem tree \(typically fetched from a Git repository or a tarball\)
, that contains a file named flake.nix in the root directory.
, flake.nix specifies some metadata about the flake such as dependencies \(called inputs\),
, as well as its outputs \(the Nix values such as packages or NixOS modules provided by the flake\).
p
h So, what does a flake look like?
x pygmentize "$(dirname "${BASH_SOURCE[0]}")/flake.nix"
h Any inputs are pinned with a lock file
x pygmentize -l json "$(dirname "${BASH_SOURCE[0]}")/flake.lock"