3. Loading repositories

These tutorials are follow-ups to Creating and configuring a session, you can prepend it directly for a functional piece of code.

3.1. Load repositories from system configuration files

 1auto repo_sack = base.get_repo_sack();
 2
 3// Create repositories from system configuration files.
 4repo_sack->create_repos_from_system_configuration();
 5
 6// If out of date, downloads fresh repositories' metadata and loads the
 7// repositories into memory.
 8//
 9// load_repos() without any arguments says to load both the @System
10// repository (the packages installed on the system, loaded from the rpmdb)
11// and the available repositories.
12repo_sack->load_repos();

3.2. Load a custom repository

 1auto repo_sack = base.get_repo_sack();
 2
 3// Create a new repo with the given id.
 4//
 5// The repo is a weak pointer to an object owned by the repo sack.
 6auto repo = repo_sack->create_repo("my_new_repo_id");
 7
 8// Configure the repo.
 9//
10// Setting at least one of the baseurl, mirrorlist or metalink options is
11// mandatory.
12//
13// baseurl examples:
14// * /absolute/path/
15// * file:///absolute/path/url/
16// * https://example.com/url/
17repo->get_config().get_baseurl_option().set(baseurl);
18
19// If out of date, downloads fresh metadata of all available repositories and
20// loads the repositories into memory.
21//
22// `libdnf5.repo.Repo.Type_AVAILABLE` as first argument says to load only the available
23// repositories (repository SYSTEM, that contains installed pacakges, is not loaded).
24repo_sack->load_repos(libdnf5::repo::Repo::Type::AVAILABLE);