How to include static data files into luarock? - lua

This is my first time creating a luarock and writing .rockpec file. I have a small lua script and some static text files that this script requires to use. How should I pack my luarock so that these static files are available for my script?
For example, here is my myscript.rockspeck file:
package = "myscript"
version = "1.0-1"
source = {
url = "https://github.com/me/myscript/raw/master/myscript-1.0.tar.gz",
tag = "v1.0"
}
description = {
summary = "My script.",
detailed = [[
Some lua script.
]],
homepage = "https://github.com/me/myscript",
license = "MIT"
}
dependencies = {
"lua >= 5.1, < 5.4"
}
build = {
type = "builtin",
modules = {
myfun = "src/myscript.lua"
},
copy_directories = {"my_data"}
}
I am able to do :
luarocks pack myscript-1.0-1.rockspec
sudo luarocks install myscript-1.0-1.src.rock
However, upon importing myfun module I can see that the my_data directory with the required files inside is not accessible by the module as it complains accordingly.
When I do laurocks pack my working directory contains myscript-1.0.tar.gz with the following sctructure:
myscript-1.0/
src/myscript.lua
tiny_data/
data1.txt
data2.txt
How should I correctly include my_data static files?

Related

Nix Flake get a specific python version

I am trying to make a Nix flake which will use poetry2nix for building a poetry project the code is
{
description = "searx : flake";
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-20.03;
outputs = { self, nixpkgs }: {
defaultPackage.x86_64-linux =
with import nixpkgs { system = "x86_64-linux";
buildInputs = [
nixpkgs.python39
nixpkgs.git
nixpkgs.openssl
nixpkgs.python39Packages.pip
nixpkgs.uwsgi
nixpkgs.python39Packages.virtualenv
nixpkgs.poetry
];
};
pkgs.poetry2nix.mkPoetryApplication {
projectDir = ./.;
src = pkgs.fetchgit {
url = "https://github.com/searx/searx.git";
rev = "ae122ea943f77600fd97556503c483dcd92e1e63";
sha256 = "sIJ+QXwUdsRIpg6ffUS3ItQvrFy0kmtI8whaiR7qEz4=";
};
};
};
However the Nix flake starts pulling python3.7 and breaks with following error
ERROR: Could not find a version that satisfies the requirement importlib-metadata; python_version < "3.8" (from click==8.0.1) (from versions: none)
> ERROR: No matching distribution found for importlib-metadata; python_version < "3.8" (from click==8.0.1)
Equivalent default.nix pulls python 3.9 and builds successfully. I believe if flake could be pinned to python3.9 it will be able to build this. Is this possible?
You can pass another python to mkPoetryApplication.
pkgs.poetry2nix.mkPoetryApplication {
projectDir = ./.;
python = pkgs.python39;
# ... your code ...
}

How can I override a package source in Nix?

So I want to replace pkgs.picom in my home-manager config with a newer fork. How can I do that?
I have a feeling it's something like:
let newPicom = pkgs.picom.override.src.url = "https://github.com/ibhagwan/picom";
in
services.picom.package = newPicom;
But knowing Nix is probably actually some really long incantation with self: super: and so on.
nixos.wiki has an example of overriding the source of a package.
You do need to provide a reproducible source. A github repo url is mutable, so you need to specify the revision.
{ pkgs, ... }:
let newPicom = pkgs.picom.overrideAttrs (old: {
version = "git"; # usually harmless to omit
src = /* put your source here; typically a local path or
a fixed-output derivation produced by
`fetchFromGitHub`.
builtins.fetchGit is also an option. Doesn't run
in parallel but does fetch private sources. */;
});
in {
services.picom.package = newPicom;
}
Overlays
let
picom_overlay = (self: super: {
picom = super.picom.overrideAttrs (prev: {
version = "git";
src = pkgs.fetchFromGitHub {
owner = "yshui";
repo = "picom";
rev = "31e58712ec11b198340ae217d33a73d8ac73b7fe";
sha256 = pkgs.lib.fakeSha256;
};
});
});
in
nixpkgs.overlays = [ picom_overlay ];
Of course, sha256 should be replaced with the relevant hash shown in the output error after building -- in this case:
sha256 = "sha256-VBnIzisg/7Xetd/AWVHlnaWXlxX+wqeYTpstO6+T5cE=";
picom-next
Note that there is also a picom-next package so one can alternatively do:
let
picom_overlay = (self: super: {
picom = super.picom.overrideAttrs (oldAttrs: rec {
inherit (super.picom-next) pname version src;
});
});
in
nixpkgs.overlays = [ picom_overlay ];
Or more simply with #RobertHensing's suggestion:
services.picom.package = pkgs.picom-next;

How can I enable Caddy plugins in NixOS?

I've just started playing with NixOS, and have so far managed to edit /etc/nixos/configuration.nix in my NixOS 18.09 VM to have PHP-FPM and the Caddy webserver enabled.
{ config, pkgs, ... }:
{
imports = [ <nixpkgs/nixos/modules/installer/virtualbox-demo.nix> ];
users = {
mutableUsers = false;
groups = {
caddy = { };
php-project = { };
};
users = {
hello = {
group = "php-project";
};
};
};
environment.systemPackages = [
pkgs.htop
pkgs.httpie
pkgs.php # for PHP CLI
];
services.caddy = {
enable = true;
email = "david#example.com";
agree = true;
config = ''
(common) {
gzip
header / -Server
header / -X-Powered-By
}
:8080 {
root /var/www/hello
fastcgi / /run/phpfpm/hello.sock php
log syslog
import common
}
'';
};
services.phpfpm = {
phpOptions = ''
date.timezone = "Europe/Berlin"
'';
poolConfigs = {
hello = ''
user = hello
listen = /run/phpfpm/hello.sock
; ...
pm.max_requests = 500
'';
};
};
}
A PHP-processed response is available at at localhost:8080. (Yay!)
To enable Caddy plugins when compiling from source, Go imports are added to caddy's run.go, e.g.:
_ "github.com/mholt/caddy/caddyhttp" // plug in the HTTP server type
// This is where other plugins get plugged in (imported)
_ "github.com/nicolasazrak/caddy-cache" // added to use another plugin
)
How can I set such line insertion to be performed after the source is downloaded and before the build takes place? (If this is a reasonable approach when using Nix?)
The NixOS 18.09 caddy package.
The NixOS 18.09 caddy service.
I believe that when writing a package a builder script (Bash or otherwise) can be assigned, and I'm thinking the line insertion could be done in it. But I'm lost as to how to assign a script to an existing package in this situation (override an attribute/use an overlay?) and where to put the script on the disk.
Status update
I've been doing some reading on customising packages in general and it sounds like overlays might be what I need. However, I don't seem to be able to get my overlay evaluated.
I'm using overriding of the package name as a test as it's simpler than patching code.
Overlay attempt 1
/etc/nixos/configuration.nix:
{ config, pkgs, options, ... }:
{
imports = [ <nixpkgs/nixos/modules/installer/virtualbox-demo.nix> ];
nix.nixPath = options.nix.nixPath.default ++ [
"nixpkgs-overlays=/etc/nixos/overlays-compat/"
];
# ...
}
/etc/nixos/overlays-compat/overlays.nix:
self: super:
with super.lib;
let
# Using the nixos plumbing that's used to evaluate the config...
eval = import <nixpkgs/nixos/lib/eval-config.nix>;
# Evaluate the config,
paths = (eval {modules = [(import <nixos-config>)];})
# then get the `nixpkgs.overlays` option.
.config.nixpkgs.overlays
;
in
foldl' (flip extends) (_: super) paths self
/etc/nixos/overlays-compat/caddy.nix:
self: super:
{
caddy = super.caddy.override {
name = "caddy-override";
};
}
Overlay attempt 2
/etc/nixos/configuration.nix:
nixpkgs.overlays = [ (self: super: {
caddy = super.caddy.override {
name = "caddy-override";
};
} ) ];
error: anonymous function at /nix/store/mr5sfmz6lm5952ch5q6v49563wzylrkx-nixos-18.09.2327.37694c8cc0e/nixos/pkgs/servers/caddy/default.nix:1:1 called with unexpected argument 'name', at /nix/store/mr5sfmz6lm5952ch5q6v49563wzylrkx-nixos-18.09.2327.37694c8cc0e/nixos/lib/customisation.nix:69:12
overrideAttrs
I previously managed to override the package name with this:
{ config, pkgs, options, ... }:
let
caddyOverride = pkgs.caddy.overrideAttrs (oldAttrs: rec {
name = "caddy-override-v${oldAttrs.version}";
});
in {
{
# ...
services.caddy = {
package = caddyOverride;
# ...
}
}
I could see in htop that the caddy binary was in a folder called /nix/store/...-caddy-override-v0.11.0-bin/. But I understand that overriding in this way has been superseded by overlays.
In order to add plugins to Caddy, it seems that the method is to modify the source.
You will need to adapt the Nixpkgs expression for Caddy to make that possible. That can be done outside the Nixpkgs tree, using services.caddy.package = callPackage ./my-caddy.nix {} for example, or by forking the Nixpkgs repository and pointing your NIX_PATH to your clone.
There is an issue for Caddy plugins: https://github.com/NixOS/nixpkgs/issues/14671
PR welcome!

Luarock: Copying .lua files to a directory when make'ing

Following this example, I've just created a rockspec for a rock with just .lua files. I don't need to build anything, so I set the build option to
build = {
type = "none",
install = {
lua = {
"a.lua",
"b.lua",
...
}
}
}
When I run luarocks make it works. However, I noticed that all the files are dumped into my /home/<username>/torch/install/share/lua/5.1/ directory. I'd like for them to be in ../share/lua/5.1/<package_name> directory. I tried doing something like
lua = {
["<package_name>"] = "a.lua",
...
or
lua = {
["<package_name>.<package_name>"] = "a.lua",
...
but neither method works.
Is there a way to put these files in a directory in the rockspec?
It's easy using the builtin build mode of rockspecs:
-- ...
build = {
type = "builtin",
modules = {
["mypackage.a"] = "a.lua",
["mypackage.b"] = "b.lua"
}
}
This should install a.lua as .../share/lua/5.1/mypackage/a.lua and b.lua as .../share/lua/5.1/mypackage/b.lua, so that require("mypackage.a") (or require("mypackage.b")) just works.

Install resources with code in luarocks

I need to embed resources (html templates) within a lua rock, as they are required by the program. But i cannot find where to describe them in the configuration.
Trying to put them in build.install.con key (as below) does not work, because the files are then stored in a "flat" manner, losing the directories.
{
package = "...",
version = "master-1",
source = { ... },
description = { ... },
dependencies = { ... },
build = {
type = "builtin",
modules = { ... },
install = {
bin = { ...},
conf = { RESOURCES },
},
},
}
Is there a way to specify resources? And where are they installed if it is possible?
There is the build.copy_directories directive (see here), which is an array of directory names that shall be copied from the source directory into the rocks tree.
You might also be interested in the datafile module, which helps with loading resources from various locations (including a rocks tree).

Resources